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