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 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> 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 <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 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); } }