/// <summary> New file </summary> public AttachmentFile(IAttachmentDestination model, HttpPostedFile postedFile) { string attachmentPath = model.AttachmentPath; FileName = VerifyFileName(attachmentPath, postedFile.FileName); FilePath = Path.Combine(attachmentPath, FileName); ContentType = postedFile.ContentType; ContentLength = postedFile.ContentLength; postedFile.SaveAs(FilePath); // write file to disk }
/// <summary> Existing file </summary> public AttachmentFile(IAttachmentDestination attachment, Data.AttachmentProxy proxy) { FileInfo file = new FileInfo(proxy.Path); DirectoryInfo dir = file.Directory; string dirName = file.Directory.ToString(); if (!dirName.Equals(attachment.AttachmentPath)) { throw new Exception($"File path {file.Directory.Name} != {attachment.AttachmentPath}"); } FileName = proxy.FileName; FilePath = proxy.Path; ContentLength = proxy.FileSize; ContentType = proxy.FileType; }
public static void CreateAttachment(AttachmentProxy attachment) { try { using (ConnectionContext connection = new ConnectionContext()) { IAttachmentDestination model = ClassFactory(connection, attachment); Data_API.Create(model as IDNode, attachment); } } catch (Exception ex) { // TODO - tell user we failed Data_API.LogMessage(ActionLogType.Insert, (ReferenceType)attachment.RefType, 0, "choke", ex); } }
/// <summary> /// CREATE /// </summary> public static void Create <T>(T proxy) { try { using (ConnectionContext connection = new ConnectionContext()) { switch (typeof(T).Name) { case "ActionProxy": { ActionProxy actionProxy = proxy as ActionProxy; Data_API.Create(connection.Ticket(actionProxy.TicketID), actionProxy); } break; case "AttachmentProxy": { AttachmentProxy attachment = proxy as AttachmentProxy; IAttachmentDestination model = AttachmentAPI.ClassFactory(connection, attachment); Data_API.Create(model as IDNode, attachment); } break; default: if (Debugger.IsAttached) { Debugger.Break(); } break; } } } catch (Exception ex) { // TODO - tell user we failed to read Data_API.LogMessage(ActionLogType.Insert, ReferenceType.None, 0, "choke", ex); } }
/// <summary> Create attachment files </summary> public static List <AttachmentProxy> CreateAttachments(HttpContext context, out string _ratingImage) { List <AttachmentProxy> result = null; try { GetPathMap(context, out PathMap pathMap, out int refID, out _ratingImage); using (ConnectionContext connection = new ConnectionContext()) { // valid ID to add attachment IAttachmentDestination model = ClassFactory(connection, pathMap._refType, refID); HttpFileCollection files = context.Request.Files; result = new List <AttachmentProxy>(); for (int i = 0; i < files.Count; i++) { if (files[i].ContentLength <= 0) { continue; } AttachmentFile attachmentFile = new AttachmentFile(model, files[i]); // create the file AttachmentProxy proxy = AttachmentProxy.ClassFactory(pathMap._refType); // construct the proxy proxy.RefID = refID; InitializeProxy(context, connection, attachmentFile, proxy); Data_API.Create(model as IDNode, proxy); // save proxy to DB result.Add(proxy); } } } catch (Exception ex) { _ratingImage = null; // TODO - tell user we failed Data_API.LogMessage(ActionLogType.Insert, ReferenceType.None, 0, "choke", ex); } return(result); }
public static void CreateAttachment(string savePath, int organizationId, AttachmentProxy.References refType, int userID, System.Net.HttpWebResponse httpWebResponse) { try { using (ConnectionContext connection = new ConnectionContext()) { AttachmentProxy attachment = AttachmentProxy.ClassFactory(refType); attachment.RefID = userID; attachment.OrganizationID = organizationId; attachment.FileName = Path.GetFileName(savePath); attachment.Path = savePath; attachment.FileType = string.IsNullOrEmpty(httpWebResponse.ContentType) ? "application/octet-stream" : httpWebResponse.ContentType; attachment.FileSize = httpWebResponse.ContentLength; IAttachmentDestination model = ClassFactory(connection, attachment); Data_API.Create(model as IDNode, attachment); } } catch (Exception ex) { // TODO - tell user we failed Data_API.LogMessage(ActionLogType.Insert, (ReferenceType)refType, 0, "choke", ex); } }
private static bool TryCreateAttachment(IAttachmentDestination attachedTo, AttachmentProxy tProxy, out IDNode result) { if ((attachedTo == null) || (tProxy == null)) { result = null; return(false); } IDNode idNode = attachedTo as IDNode; tProxy.DateCreated = tProxy.DateModified = DateTime.UtcNow; tProxy.CreatorID = tProxy.ModifierID = idNode.Connection.UserID; tProxy.OrganizationID = idNode.Connection.OrganizationID; switch (tProxy) // alphabetized list { case ActionAttachmentProxy proxy: // create action attachment proxy.RefID = (idNode as ActionModel).ActionID; break; case AssetAttachmentProxy proxy: // create asset attachment proxy.RefID = (idNode as AssetModel).AssetID; break; case ChatAttachmentProxy proxy: // create asset attachment proxy.RefID = (idNode as ChatModel).ChatID; break; case CompanyActivityAttachmentProxy proxy: proxy.RefID = (idNode as NoteModel).NoteID; break; case ContactActivityAttachmentProxy proxy: proxy.RefID = (idNode as NoteModel).NoteID; break; case OrganizationAttachmentProxy proxy: proxy.RefID = (idNode as OrganizationModel).OrganizationID; break; case ProductVersionAttachmentProxy proxy: proxy.RefID = (idNode as ProductVersionModel).ProductVersionID; break; case TaskAttachmentProxy proxy: proxy.RefID = (idNode as TaskModel).TaskID; break; case UserAttachmentProxy proxy: // create action attachment proxy.RefID = (idNode as UserModel).UserID; break; case UserPhotoAttachmentProxy proxy: // create action attachment proxy.RefID = (idNode as UserModel).UserID; break; case WatercoolerMsgAttachmentProxy proxy: // create action attachment proxy.RefID = (idNode as WatercoolerMsgModel).MessageID; break; case null: default: if (Debugger.IsAttached) { Debugger.Break(); } break; } Table <AttachmentProxy> table = idNode.Connection._db.GetTable <AttachmentProxy>(); table.InsertOnSubmit(tProxy); idNode.Connection._db.SubmitChanges(); result = new AttachmentModel(attachedTo, tProxy.AttachmentID); // disable Verify? return(true); }
public static string DeleteAttachment(int attachmentID, AttachmentProxy.References verifyRefType, int?verifyRefID = null) { string result = null; try { using (ConnectionContext connection = new ConnectionContext()) { // validate args AttachmentProxy proxy = Data_API.ReadRefTypeProxy <AttachmentProxy>(connection, attachmentID); result = proxy.FileName; if (verifyRefType == AttachmentProxy.References.None) // support delete no matter the type? { verifyRefType = proxy.RefType; } if (!verifyRefID.HasValue) // if no explicit refID provided, use the proxy { verifyRefID = proxy.RefID; } // type safe construction IAttachmentDestination model = null; switch (verifyRefType) { case AttachmentProxy.References.Actions: model = new ActionModel(connection, verifyRefID.Value); if (!(model as ActionModel).CanEdit()) // is user authorized to do the delete? { return(null); } break; case AttachmentProxy.References.Assets: model = new AssetModel(connection, verifyRefID.Value); break; //case AttachmentProxy.References.ChatAttachments: // break; case AttachmentProxy.References.CompanyActivity: { int organizationID = connection._db.ExecuteQuery <int>($"Select RefID from Notes WITH (NOLOCK) WHERE NoteID = {verifyRefID.Value}").Min(); model = new NoteModel(new OrganizationModel(connection, organizationID), verifyRefID.Value); } break; case AttachmentProxy.References.ContactActivity: { int userID = connection._db.ExecuteQuery <int>($"Select RefID from Notes WITH (NOLOCK) WHERE NoteID = {verifyRefID.Value}").Min(); model = new NoteModel(new UserModel(connection, userID), verifyRefID.Value); } break; //case AttachmentProxy.References.Contacts: // break; //case AttachmentProxy.References.CustomerHubLogo: // break; //case AttachmentProxy.References.None: // break; case AttachmentProxy.References.Organizations: model = new OrganizationModel(connection, verifyRefID.Value); break; case AttachmentProxy.References.ProductVersions: model = new ProductVersionModel(connection, verifyRefID.Value); break; case AttachmentProxy.References.Tasks: model = new TaskModel(connection, verifyRefID.Value); break; case AttachmentProxy.References.UserPhoto: case AttachmentProxy.References.Users: model = new UserModel(connection, verifyRefID.Value); break; case AttachmentProxy.References.WaterCooler: model = new WatercoolerMsgModel(connection, verifyRefID.Value); break; default: if (Debugger.IsAttached) { Debugger.Break(); } throw new Exception($"unrecognized RefType {verifyRefType} in DeleteAttachment"); } // do the delete Data_API.Delete(new AttachmentModel(model, attachmentID)); AttachmentFile file = new AttachmentFile(model, proxy); file.Delete(); } } catch (Exception ex) { Data_API.LogMessage(ActionLogType.Delete, ReferenceType.Attachments, attachmentID, "Unable to delete attachment", ex); } return(result); }
public AttachmentModel(IAttachmentDestination attachedTo, int id) : base((attachedTo as IDNode).Connection) { AttachmentID = id; AttachedTo = attachedTo; Verify(); }