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