public object Post(PostAttachment request) { var response = new CommonResponse(); string attachmentKind = request.AttachmentKind; if (attachmentKind == null || attachmentKind == "") { response.ErrorThrown = true; response.ResponseDescription = "Attachment Kind was not specified."; return(response); } if (Request.Files.Length == 0) { throw new HttpError(HttpStatusCode.BadRequest, "NoFile"); } var postedFile = Request.Files[0]; string fileName = postedFile.FileName; string baseAttachmentsPath = AppSettings.Get <string>(attachmentKind); bool useAttachmentsRelativePath = false; string sUseAttachmentsRelativePath = AppSettings.Get <string>("UseAttachmentsRelativePath"); if (!string.IsNullOrWhiteSpace(sUseAttachmentsRelativePath) && bool.TryParse(sUseAttachmentsRelativePath, out bool bUseAttachmentsRelativePath)) { useAttachmentsRelativePath = bUseAttachmentsRelativePath; } string currentPathAttachments; string folderName = request.TargetFolder; if (!string.IsNullOrWhiteSpace(folderName)) { if (useAttachmentsRelativePath) { currentPathAttachments = "~/" + baseAttachmentsPath + folderName + @"/".MapHostAbsolutePath(); } else { currentPathAttachments = baseAttachmentsPath + folderName + @"\"; } if (!Directory.Exists(currentPathAttachments)) { Directory.CreateDirectory(currentPathAttachments); } } else { string folderPrefix = request.FolderPrefix; if (string.IsNullOrWhiteSpace(folderPrefix) || folderPrefix == "undefined" || folderPrefix == "null") { folderPrefix = ""; } do { DateTime date = DateTime.Now; folderName = folderPrefix + date.ToString("yy") + date.Month.ToString("d2") + date.Day.ToString("d2") + "_" + MD5HashGenerator.GenerateKey(date); if (useAttachmentsRelativePath) { currentPathAttachments = "~/" + baseAttachmentsPath + folderName.MapHostAbsolutePath(); } else { currentPathAttachments = baseAttachmentsPath + folderName; } } while (Directory.Exists(currentPathAttachments)); Directory.CreateDirectory(currentPathAttachments); if (useAttachmentsRelativePath) { currentPathAttachments += @"/"; } else { currentPathAttachments += @"\"; } } if (postedFile.ContentLength > 0) { postedFile.SaveTo(currentPathAttachments + Path.GetFileName(postedFile.FileName)); } var attachmentsResult = AttachmentsIO.getAttachmentsFromFolder(folderName, attachmentKind); response.ErrorThrown = false; response.ResponseDescription = folderName; response.Result = attachmentsResult; return(response); }
public object Post(PostAttachment request) { var AttachmentKind = request.AttachmentKind; if (!IsValidJSValue(AttachmentKind)) { throw new KnownError("Invalid [Attachment Kind]."); } if (Request.Files.Length == 0) { throw new HttpError(HttpStatusCode.BadRequest, "NoFile"); } var postedFile = Request.Files[0]; string FileName = postedFile.FileName; string baseAttachmentsPath = AppSettings.Get <string>(AttachmentKind); if (string.IsNullOrWhiteSpace(baseAttachmentsPath)) { throw new KnownError("Invalid Attachment Kind."); } bool useAttachmentsRelativePath = false; string sUseAttachmentsRelativePath = AppSettings.Get <string>("UseAttachmentsRelativePath"); if (!string.IsNullOrWhiteSpace(sUseAttachmentsRelativePath) && bool.TryParse(sUseAttachmentsRelativePath, out bool bUseAttachmentsRelativePath)) { useAttachmentsRelativePath = bUseAttachmentsRelativePath; } string currentPathAttachments; string folderName = request.TargetFolder; if (IsValidJSValue(folderName)) { if (useAttachmentsRelativePath) { currentPathAttachments = "~/" + baseAttachmentsPath + folderName + @"/".MapHostAbsolutePath(); } else { currentPathAttachments = baseAttachmentsPath + folderName + @"\"; } if (!Directory.Exists(currentPathAttachments)) { Directory.CreateDirectory(currentPathAttachments); } } else { string folderPrefix = request.FolderPrefix; if (!IsValidJSValue(folderPrefix)) { folderPrefix = ""; } do { DateTime date = DateTime.Now; folderName = folderPrefix + date.ToString("yy") + date.Month.ToString("d2") + date.Day.ToString("d2") + "_" + MD5HashGenerator.GenerateKey(date); if (useAttachmentsRelativePath) { currentPathAttachments = "~/" + baseAttachmentsPath + folderName.MapHostAbsolutePath(); } else { currentPathAttachments = baseAttachmentsPath + folderName; } } while (Directory.Exists(currentPathAttachments)); Directory.CreateDirectory(currentPathAttachments); if (useAttachmentsRelativePath) { currentPathAttachments += @"/"; } else { currentPathAttachments += @"\"; } } if (postedFile.ContentLength > 0) { Log.Info($"Attachment Posted: [{currentPathAttachments + Path.GetFileName(postedFile.FileName)}] by User: [{GetSession().UserName}]"); postedFile.SaveTo(currentPathAttachments + Path.GetFileName(postedFile.FileName)); } return(new // FileId { FileName, AttachmentKind, Directory = folderName }); }