public async Task <SuggestionModel> AddAsync(SuggestionModel suggestionModel, string mediaFolderPath, string prefixSuggestionFileName) { var mediaAbsoluteBasePath = Path.Combine(env.WebRootPath, mediaFolderPath.Replace("~/", string.Empty)); string newFileName = null; try { if (suggestionModel == null) { throw new ArgumentNullException("suggestionModel"); } if (OssFile.HasFile(suggestionModel.FileUploaded)) { newFileName = await OssFile.SaveFile(suggestionModel.FileUploaded, prefixSuggestionFileName, mediaAbsoluteBasePath);; } else { if (!string.IsNullOrEmpty(suggestionModel.FileUploadedName) && File.Exists(Path.Combine(mediaAbsoluteBasePath, suggestionModel.FileUploadedName))) { newFileName = OssFile.GetNewFileName(suggestionModel.FileUploadedName, prefixSuggestionFileName); File.Move ( Path.Combine(mediaAbsoluteBasePath, suggestionModel.FileUploadedName), Path.Combine(mediaAbsoluteBasePath, newFileName) ); } } Suggestion newSuggestion = new Suggestion ( suggestionModel.Id, suggestionModel.Subject, suggestionModel.Message, newFileName, suggestionModel.IsRead, suggestionModel.IsSolved, DateTime.UtcNow, suggestionModel.UserModel.Id, null ); biblioEntities.Suggestions.Add(newSuggestion); await biblioEntities.SaveChangesAsync(); return(new SuggestionModel(newSuggestion, mediaFolderPath, mediaFolderPath)); } catch (Exception ex) { if (OssFile.HasFile(suggestionModel.FileUploaded) && !string.IsNullOrEmpty(newFileName)) { OssFile.DeleteFile(newFileName, mediaAbsoluteBasePath); } throw ex; } }
public void FileUpload() { try { int fileid = 0; var files = Request.Form.Files; if (files.Count > 0) { var file = files.First(); if (file.Length > (200 * 1024 * 1024)) { //return Json("文件过大,不能大于200M"); } PutExtra putExtra = new PutExtra();// { ProgressHandler = UploadProgressHandler, ResumeRecordFile }; putExtra.ProgressHandler = UploadProgressHandler; putExtra.UploadController = DefaultUploadController; putExtra.ResumeRecordFile = ResumeHelper.GetDefaultRecordKey($"{Environment.CurrentDirectory}\\{file.FileName}", file.FileName); var aa = putExtra.ResumeRecordFile; putExtra.ResumeRecordFile = file.FileName + ".progress"; HttpResult result = new HttpResult(); Stream stream = file.OpenReadStream(); result = qiniu.UploadStream(file.FileName, stream, putExtra); if (result.Code == (int)HttpCode.OK) { //todo 入库文件信息 var rj = JObject.Parse(result.Text); string hash = rj["hash"].ToString(); string key = rj["key"].ToString(); var hashentity = ossfile.GetList(t => t.FileHash == hash); OssFile ossFileEntity = new OssFile { FileKey = key, FileHash = hash, FileDomain = "" }; ossfile.Insert_Return_Id(ossFileEntity, out fileid); } //检查删除文件 if (System.IO.File.Exists($"{Environment.CurrentDirectory}\\{file.FileName}")) { System.IO.File.Delete($"{Environment.CurrentDirectory}\\{file.FileName}"); } } } catch (Exception ex) { // return Json(ex.Message); } }
public async Task RemoveAsync(int id, string mediaFolderPath) { var suggestion = await biblioEntities.Suggestions.FindAsync(id); if (suggestion != null) { biblioEntities.Suggestions.Remove(suggestion); await biblioEntities.SaveChangesAsync(); if (!string.IsNullOrEmpty(suggestion.File)) { var mediaBasePath = Path.Combine(env.WebRootPath, mediaFolderPath.Replace("~/", string.Empty)); OssFile.DeleteFile(suggestion.File, mediaBasePath); OssFile.DeleteFile(suggestion.File, mediaBasePath); } } }
public async Task RemoveAsync(int id, string mediaFolderPath) { var document = await biblioEntities.Documents.FindAsync(id); if (document != null) { biblioEntities.Documents.Remove(document); await biblioEntities.SaveChangesAsync(); if (!string.IsNullOrEmpty(document.Image)) { var mediaBasePath = Path.Combine(env.WebRootPath, mediaFolderPath.Replace("~/", string.Empty)); OssFile.DeleteFile(document.Image, mediaBasePath); OssFile.DeleteFile(document.File, mediaBasePath); } } }
public async Task <DocumentModel> ExtractMetadata(DocumentModel documentModel, string PrefixDocumentTmpFileName, string mediaFolderTmpPath, string libGostScriptPath, int userId) { var mediaBaseTmpPath = Path.Combine(env.WebRootPath, mediaFolderTmpPath.Replace("~/", string.Empty).Replace("/", @"\")); var libBaseGostScriptPath = Path.Combine(env.ContentRootPath, libGostScriptPath.Replace("~/", string.Empty)); var prefixTmpThumb = "tmp_" + userId + "_"; OssFile.DeleteFileInFolder(prefixTmpThumb, mediaBaseTmpPath); string newFileName = string.Empty; if (OssFile.HasFile(documentModel.FileUploaded)) { newFileName = await OssFile.SaveFile(documentModel.FileUploaded, prefixTmpThumb, mediaBaseTmpPath);; documentModel.FileUploadedName = documentModel.FileUploaded.FileName; } string coverFileName = prefixTmpThumb + Guid.NewGuid().ToString().ToLower() + ".png"; OssFile.ConvertPdfToImage ( libBaseGostScriptPath, Path.Combine(mediaBaseTmpPath, newFileName), 1, Path.Combine(mediaBaseTmpPath, coverFileName), 400, 600 ); PdfReader reader = new PdfReader(Path.Combine(mediaBaseTmpPath, newFileName)); documentModel.Code = reader.Info["ISBN"]?.ToString() ?? documentModel.Code; documentModel.Title = reader.Info["Title"]?.ToString(); documentModel.Authors = reader.Info["Author"]?.ToString(); documentModel.Description = reader.Info["Subject"]?.ToString() + (!string.IsNullOrEmpty(reader.Info["Keywords"]?.ToString()) ? " " + reader.Info["Keywords"]?.ToString() : null); documentModel.Language = reader.Info["Language"]?.ToString(); documentModel.Publisher = reader.Info["Creator"]?.ToString(); documentModel.CategoryId = int.Parse(reader.Info["CategoryId"]?.ToString() ?? "0"); documentModel.Contributors = reader.Info["Contributors"]?.ToString(); documentModel.NumberOfPages = reader.NumberOfPages; documentModel.FileUploadedTmpFileName = newFileName; documentModel.ImageUploadedTmpFileName = coverFileName; documentModel.ImageLink = $"{mediaFolderTmpPath}/{coverFileName}"; reader.Close(); return(documentModel); }
public async Task <SuggestionModel> SetAsync(SuggestionModel suggestionModel, string mediaFolderPath, string mediaFolderTmpPath, string prefixSuggestionImageName, string prefixSuggestionFileName) { string newFileName = null; var mediaAbsoluteBasePath = Path.Combine(env.WebRootPath, mediaFolderPath?.Replace("~/", string.Empty)); try { if (suggestionModel == null) { throw new ArgumentNullException("suggestionModel"); } var currentSuggestion = await biblioEntities.Suggestions.FindAsync(suggestionModel.Id); if (currentSuggestion == null) { throw new KeyNotFoundException("Suggestion"); } bool deleteCurrentFile = false; string currentSuggestionFile = currentSuggestion.File; if (OssFile.HasFile(suggestionModel.FileUploaded)) { newFileName = await OssFile.SaveFile(suggestionModel.FileUploaded, prefixSuggestionFileName, mediaAbsoluteBasePath);; deleteCurrentFile = true; } else if (!string.IsNullOrEmpty(suggestionModel.FileUploadedName) && File.Exists(Path.Combine(mediaAbsoluteBasePath, suggestionModel.FileUploadedName))) { newFileName = OssFile.GetNewFileName(suggestionModel.FileUploadedName, prefixSuggestionFileName); File.Move ( Path.Combine(mediaAbsoluteBasePath, suggestionModel.FileUploadedName), Path.Combine(mediaAbsoluteBasePath, newFileName) ); deleteCurrentFile = true; } else { newFileName = currentSuggestion.File; } Suggestion newSuggestion = new Suggestion ( suggestionModel.Id, suggestionModel.Subject, suggestionModel.Message, newFileName, suggestionModel.IsRead, suggestionModel.IsSolved, currentSuggestion.Date, currentSuggestion.UserId, null ); biblioEntities.Entry(currentSuggestion).CurrentValues.SetValues(newSuggestion); await biblioEntities.SaveChangesAsync(); if (deleteCurrentFile) { OssFile.DeleteFile(currentSuggestionFile, mediaAbsoluteBasePath); } return(new SuggestionModel(newSuggestion, mediaFolderPath, mediaFolderPath)); } catch (Exception ex) { if (OssFile.HasFile(suggestionModel.FileUploaded) && !string.IsNullOrEmpty(newFileName)) { OssFile.DeleteFile(newFileName, mediaAbsoluteBasePath); } throw ex; } }
public async Task <DocumentModel> SetAsync(DocumentModel documentModel, string mediaFolderPath, string mediaFolderTmpPath, string prefixDocumentImageName, string prefixDocumentFileName) { string newImageName = null; string newFileName = null; var mediaAbsoluteBasePath = Path.Combine(env.WebRootPath, mediaFolderPath?.Replace("~/", string.Empty)); var mediaAbsoluteBaseTmpPath = Path.Combine(env.WebRootPath, mediaFolderTmpPath.Replace("~/", string.Empty).Replace("/", @"\")); try { if (documentModel == null) { throw new ArgumentNullException("documentModel"); } var currentDocument = await biblioEntities.Documents.FindAsync(documentModel.Id); if (currentDocument == null) { throw new KeyNotFoundException("Document"); } bool deleteCurrentIamge = false, deleteCurrentFile = false; string currentDocumentImage = currentDocument.Image; string currentDocumentFile = currentDocument.File; if (OssFile.HasImage(documentModel.ImageUploaded)) { newImageName = OssFile.SaveImage(documentModel.ImageUploaded, 400, 600, 100 * 1024, 300 * 1024, prefixDocumentImageName, mediaAbsoluteBasePath);; deleteCurrentIamge = true; } else if (string.IsNullOrEmpty(documentModel.ImageUploadedTmpFileName) && !string.IsNullOrEmpty(currentDocument.Image) && documentModel.DeleteImage) { deleteCurrentIamge = true; } else if (!documentModel.DeleteImage && !string.IsNullOrEmpty(documentModel.ImageUploadedTmpFileName) && File.Exists(Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.ImageUploadedTmpFileName))) { newImageName = OssFile.GetNewFileName(documentModel.ImageUploadedTmpFileName, prefixDocumentImageName); File.Move ( Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.ImageUploadedTmpFileName), Path.Combine(mediaAbsoluteBasePath, newImageName) ); deleteCurrentIamge = true; } else if (documentModel.DeleteImage && !string.IsNullOrEmpty(documentModel.ImageUploadedTmpFileName) && File.Exists(Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.ImageUploadedTmpFileName))) { File.Delete ( Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.ImageUploadedTmpFileName) ); newImageName = currentDocument.Image; } else { newImageName = currentDocument.Image; } if (OssFile.HasFile(documentModel.FileUploaded)) { newFileName = await OssFile.SaveFile(documentModel.FileUploaded, prefixDocumentFileName, mediaAbsoluteBasePath);; deleteCurrentFile = true; } else if (!string.IsNullOrEmpty(documentModel.FileUploadedTmpFileName) && File.Exists(Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.FileUploadedTmpFileName))) { newFileName = OssFile.GetNewFileName(documentModel.FileUploadedTmpFileName, prefixDocumentFileName); File.Move ( Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.FileUploadedTmpFileName), Path.Combine(mediaAbsoluteBasePath, newFileName) ); deleteCurrentFile = true; } else { newFileName = currentDocument.File; } Document newDocument = new Document ( documentModel.Id, documentModel.Code, documentModel.Title, documentModel.Subtitle, documentModel.Authors, documentModel.Description, documentModel.Language, documentModel.PublishDate, documentModel.Publisher, documentModel.NumberOfPages, documentModel.Contributors, documentModel.CategoryId, null, newFileName, newImageName, currentDocument.CreateDate, (short)documentModel.Status ); biblioEntities.Entry(currentDocument).CurrentValues.SetValues(newDocument); await biblioEntities.SaveChangesAsync(); if (deleteCurrentIamge) { OssFile.DeleteFile(currentDocumentImage, mediaAbsoluteBasePath); } if (deleteCurrentFile) { OssFile.DeleteFile(currentDocumentFile, mediaAbsoluteBasePath); } return(new DocumentModel(newDocument, mediaFolderPath, mediaFolderPath, null, 0)); } catch (Exception ex) { if (OssFile.HasImage(documentModel.ImageUploaded) && !string.IsNullOrEmpty(newImageName)) { OssFile.DeleteFile(newImageName, mediaAbsoluteBasePath); } if (OssFile.HasFile(documentModel.FileUploaded) && !string.IsNullOrEmpty(newFileName)) { OssFile.DeleteFile(newFileName, mediaAbsoluteBasePath); } throw ex; } }
public async Task <DocumentModel> AddAsync(DocumentModel documentModel, string mediaFolderPath, string mediaFolderTmpPath, string prefixDocumentImageName, string prefixDocumentFileName) { var mediaAbsoluteBasePath = Path.Combine(env.WebRootPath, mediaFolderPath.Replace("~/", string.Empty)); var mediaAbsoluteBaseTmpPath = Path.Combine(env.WebRootPath, mediaFolderTmpPath.Replace("~/", string.Empty).Replace("/", @"\")); string newImageName = null; string newFileName = null; try { if (documentModel == null) { throw new ArgumentNullException("documentModel"); } if (OssFile.HasImage(documentModel.ImageUploaded)) { newImageName = OssFile.SaveImage(documentModel.ImageUploaded, 400, 600, 100 * 1024, 300 * 1024, prefixDocumentImageName, mediaAbsoluteBasePath);; } else { if (!documentModel.DeleteImage) { if (!string.IsNullOrEmpty(documentModel.ImageUploadedTmpFileName) && File.Exists(Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.ImageUploadedTmpFileName))) { newImageName = OssFile.GetNewFileName(documentModel.ImageUploadedTmpFileName, prefixDocumentImageName); File.Move ( Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.ImageUploadedTmpFileName), Path.Combine(mediaAbsoluteBasePath, newImageName) ); } } else { if (!string.IsNullOrEmpty(documentModel.ImageUploadedTmpFileName) && File.Exists(Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.ImageUploadedTmpFileName))) { File.Delete ( Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.ImageUploadedTmpFileName) ); } } } if (OssFile.HasFile(documentModel.FileUploaded)) { newFileName = await OssFile.SaveFile(documentModel.FileUploaded, prefixDocumentFileName, mediaAbsoluteBasePath);; } else { if (!string.IsNullOrEmpty(documentModel.FileUploadedTmpFileName) && File.Exists(Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.FileUploadedTmpFileName))) { newFileName = OssFile.GetNewFileName(documentModel.FileUploadedTmpFileName, prefixDocumentFileName); File.Move ( Path.Combine(mediaAbsoluteBaseTmpPath, documentModel.FileUploadedTmpFileName), Path.Combine(mediaAbsoluteBasePath, newFileName) ); } } Document newDocument = new Document ( documentModel.Id, documentModel.Code, documentModel.Title, documentModel.Subtitle, documentModel.Authors, documentModel.Description, documentModel.Language, documentModel.PublishDate, documentModel.Publisher, documentModel.NumberOfPages, documentModel.Contributors, documentModel.CategoryId, null, newFileName, newImageName, DateTime.UtcNow, (short)documentModel.Status ); biblioEntities.Documents.Add(newDocument); await biblioEntities.SaveChangesAsync(); return(new DocumentModel(newDocument, mediaFolderPath, mediaFolderPath, null, 0)); } catch (Exception ex) { if (OssFile.HasImage(documentModel.ImageUploaded) && !string.IsNullOrEmpty(newImageName)) { OssFile.DeleteFile(newImageName, mediaAbsoluteBasePath); } if (OssFile.HasFile(documentModel.FileUploaded) && !string.IsNullOrEmpty(newFileName)) { OssFile.DeleteFile(newFileName, mediaAbsoluteBasePath); } throw ex; } }
public async Task UpdateMetaData(string mediaFolderPath, string prefixDocumentFileName, DocumentModel documentModel) { try { var document = await biblioEntities.Documents .SingleOrDefaultAsync(x => x.Id == documentModel.Id); if (document != null) { string inputFile = Path.Combine(env.WebRootPath, documentModel.FileLink.Replace("~/", string.Empty).Replace("/", @"\")); string mediaBasePath = Path.Combine(env.WebRootPath, mediaFolderPath.Replace("~/", string.Empty)); string newFileName = OssFile.GetNewFileName(inputFile, prefixDocumentFileName); string outputFile = Path.Combine(mediaBasePath, newFileName); using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) { PdfReader reader = new PdfReader(inputFile); PdfStamper stamper = new PdfStamper(reader, fs); Hashtable info = reader.Info; if (info.ContainsKey("ISBN")) { info.Remove("ISBN"); } info.Add("ISBN", documentModel.Code); if (info.ContainsKey("Title")) { info.Remove("Title"); } info.Add("Title", documentModel.Title); if (info.ContainsKey("Subtitle")) { info.Remove("Subtitle"); } info.Add("Subtitle", documentModel.Subtitle); if (info.ContainsKey("Subject")) { info.Remove("Subject"); } info.Add("Subject", documentModel.Description); if (info.ContainsKey("Creator")) { info.Remove("Creator"); } info.Add("Creator", documentModel.Publisher); if (documentModel.PublishDate.HasValue) { if (info.ContainsKey("PublishDate")) { info.Remove("PublishDate"); } info.Add("PublishDate", documentModel.PublishDate.Value.ToString("yyyy-MM-dd")); } if (info.ContainsKey("Author")) { info.Remove("Author"); } info.Add("Author", documentModel.Authors); if (info.ContainsKey("Contributors")) { info.Remove("Contributors"); } info.Add("Contributors", documentModel.Contributors); if (info.ContainsKey("Language")) { info.Remove("Language"); } info.Add("Language", documentModel.Language); if (info.ContainsKey("CategoryId")) { info.Remove("CategoryId"); } info.Add("CategoryId", documentModel.CategoryId.ToString()); if (info.ContainsKey("CategoryName")) { info.Remove("CategoryName"); } info.Add("CategoryName", documentModel.CategoryName); stamper.MoreInfo = info; stamper.Close(); reader.Close(); } document.File = newFileName; await biblioEntities.SaveChangesAsync(); try { File.Delete(inputFile); } catch (Exception ex) { throw new FileLoadException(inputFile, ex); } } } catch (FileLoadException ex) { throw ex; } catch (Exception ex) { throw new MethodAccessException("UpdateMetaData", ex); } }