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