Skip to content

Commit

Permalink
Delete fs.chunks when user delete object from GrandNode #136
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Oct 11, 2021
1 parent a6d70cc commit 5afe95e
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public async Task<string> SaveOrderToBinary(Order order, string languageId, stri
ContentType = "application/pdf",
};

download.DownloadObjectId = await _storeFilesContext.BucketUploadFromBytesAsync(download.Filename, ms.ToArray());
download.DownloadObjectId = await _storeFilesContext.BucketUploadFromBytes(download.Filename, ms.ToArray());
await _downloadRepository.InsertAsync(download);

//TODO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public virtual async Task InsertDownload(Download download)
throw new ArgumentNullException(nameof(download));
if (!download.UseDownloadUrl)
{
download.DownloadObjectId = await _storeFilesContext.BucketUploadFromBytesAsync(download.Filename, download.DownloadBinary);
download.DownloadObjectId = await _storeFilesContext.BucketUploadFromBytes(download.Filename, download.DownloadBinary);
}

download.DownloadBinary = null;
Expand Down Expand Up @@ -130,6 +130,10 @@ public virtual async Task DeleteDownload(Download download)

await _downloadRepository.DeleteAsync(download);

//delete from bucket
if(!string.IsNullOrEmpty(download.DownloadObjectId))
await _storeFilesContext.BucketDelete(download.DownloadObjectId);

await _mediator.EntityDeleted(download);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Core/Grand.Domain/Data/IStoreFilesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Grand.Domain.Data
public interface IStoreFilesContext
{
Task<byte[]> BucketDownload(string id);
Task<string> BucketUploadFromBytesAsync(string filename, byte[] source);
Task BucketDelete(string id);
Task<string> BucketUploadFromBytes(string filename, byte[] source);
}
}
8 changes: 7 additions & 1 deletion src/Core/Grand.Domain/Data/Mongo/MongoStoreFilesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public async Task<byte[]> BucketDownload(string id)
return binary;

}
public async Task<string> BucketUploadFromBytesAsync(string filename, byte[] source)
public async Task BucketDelete(string id)
{
var bucket = new MongoDB.Driver.GridFS.GridFSBucket(_database);
await bucket.DeleteAsync(new ObjectId(id));
}

public async Task<string> BucketUploadFromBytes(string filename, byte[] source)
{
var bucket = new MongoDB.Driver.GridFS.GridFSBucket(_database);
var id = await bucket.UploadFromBytesAsync(filename, source);
Expand Down
27 changes: 3 additions & 24 deletions src/Web/Grand.Web.Admin/Controllers/DocumentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,7 @@ public async Task<IActionResult> CreateDocument(DocumentModel model, bool contin
{
if (ModelState.IsValid)
{
if (string.IsNullOrEmpty(model.CustomerId))
model.CustomerId = (await _customerService.GetCustomerByEmail(model.CustomerEmail))?.Id;

var document = model.ToEntity();
document.CreatedOnUtc = DateTime.UtcNow;

await _documentService.Insert(document);

//activity log
await _customerActivityService.InsertActivity("AddNewDocument", document.Id, _translationService.GetResource("ActivityLog.AddNewDocument"), document.Name);
var document = await _documentViewModelService.InsertDocument(model);

Success(_translationService.GetResource("Admin.Documents.Document.Added"));
return continueEditing ? RedirectToAction("EditDocument", new { id = document.Id }) : RedirectToAction("List");
Expand Down Expand Up @@ -112,16 +103,7 @@ public async Task<IActionResult> EditDocument(DocumentModel model, bool continue

if (ModelState.IsValid)
{
if (string.IsNullOrEmpty(model.CustomerId))
model.CustomerId = (await _customerService.GetCustomerByEmail(model.CustomerEmail))?.Id;

document = model.ToEntity(document);
document.UpdatedOnUtc = DateTime.UtcNow;

await _documentService.Update(document);

//activity log
await _customerActivityService.InsertActivity("EditDocument", document.Id, _translationService.GetResource("ActivityLog.EditDocument"), document.Name);
document = await _documentViewModelService.UpdateDocument(document, model);

Success(_translationService.GetResource("Admin.Documents.Document.Updated"));
return continueEditing ? RedirectToAction("EditDocument", new { id = document.Id }) : RedirectToAction("List");
Expand All @@ -142,10 +124,7 @@ public async Task<IActionResult> DeleteDocument(string id)

if (ModelState.IsValid)
{
await _documentService.Delete(document);

//activity log
await _customerActivityService.InsertActivity("DeleteDocument", document.Id, _translationService.GetResource("ActivityLog.DeleteDocument"), document.Name);
await _documentViewModelService.DeleteDocument(document);

Success(_translationService.GetResource("Admin.Documents.Document.Deleted"));
return RedirectToAction("List");
Expand Down
1 change: 1 addition & 0 deletions src/Web/Grand.Web.Admin/Controllers/DownloadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public async Task<IActionResult> SaveDownloadUrl(string downloadUrl)
return Json(new { downloadId = download.Id, success = true });
}

[DisableRequestSizeLimit]
[HttpPost]
//do not validate request token (XSRF)
[IgnoreAntiforgeryToken]
Expand Down
22 changes: 22 additions & 0 deletions src/Web/Grand.Web.Admin/Controllers/MessageTemplateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Grand.Business.Common.Interfaces.Stores;
using Grand.Business.Common.Services.Security;
using Grand.Business.Messages.Interfaces;
using Grand.Business.Storage.Interfaces;
using Grand.Domain.Messages;
using Grand.Web.Admin.Extensions;
using Grand.Web.Admin.Models.Messages;
Expand All @@ -29,6 +30,7 @@ public partial class MessageTemplateController : BaseAdminController
private readonly ITranslationService _translationService;
private readonly IMessageTokenProvider _messageTokenProvider;
private readonly IStoreService _storeService;
private readonly IDownloadService _downloadService;
private readonly EmailAccountSettings _emailAccountSettings;

#endregion Fields
Expand All @@ -41,6 +43,7 @@ public MessageTemplateController(IMessageTemplateService messageTemplateService,
ITranslationService translationService,
IMessageTokenProvider messageTokenProvider,
IStoreService storeService,
IDownloadService downloadService,
EmailAccountSettings emailAccountSettings)
{
_messageTemplateService = messageTemplateService;
Expand All @@ -49,6 +52,7 @@ public MessageTemplateController(IMessageTemplateService messageTemplateService,
_translationService = translationService;
_messageTokenProvider = messageTokenProvider;
_storeService = storeService;
_downloadService = downloadService;
_emailAccountSettings = emailAccountSettings;
}

Expand Down Expand Up @@ -197,6 +201,8 @@ public async Task<IActionResult> Edit(MessageTemplateModel model, bool continueE
//No message template found with the specified id
return RedirectToAction("List");

var prevAttachment = messageTemplate.AttachedDownloadId;

if (ModelState.IsValid)
{
messageTemplate = model.ToEntity(messageTemplate);
Expand All @@ -206,6 +212,14 @@ public async Task<IActionResult> Edit(MessageTemplateModel model, bool continueE
if (model.SendImmediately)
messageTemplate.DelayBeforeSend = null;

//delete an old "attachment" file
if (!string.IsNullOrEmpty(prevAttachment) && prevAttachment!= messageTemplate.AttachedDownloadId)
{
var attachment = await _downloadService.GetDownloadById(prevAttachment);
if (attachment != null)
await _downloadService.DeleteDownload(attachment);
}

await _messageTemplateService.UpdateMessageTemplate(messageTemplate);

Success(_translationService.GetResource("Admin.Content.MessageTemplates.Updated"));
Expand Down Expand Up @@ -241,6 +255,14 @@ public async Task<IActionResult> Delete(string id)

await _messageTemplateService.DeleteMessageTemplate(messageTemplate);

//delete an old "attachment" file
if (!string.IsNullOrEmpty(messageTemplate.AttachedDownloadId))
{
var attachment = await _downloadService.GetDownloadById(messageTemplate.AttachedDownloadId);
if (attachment != null)
await _downloadService.DeleteDownload(attachment);
}

Success(_translationService.GetResource("Admin.Content.MessageTemplates.Deleted"));
return RedirectToAction("List");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ public interface IDocumentViewModelService
{
Task<(IEnumerable<DocumentModel> documetListModel, int totalCount)> PrepareDocumentListModel(DocumentListModel model, int pageIndex, int pageSize);
Task<DocumentModel> PrepareDocumentModel(DocumentModel documentModel, Document document, SimpleDocumentModel simpleModel);
Task<Document> InsertDocument(DocumentModel model);
Task<Document> UpdateDocument(Document document, DocumentModel model);
Task DeleteDocument(Document document);
}
}
42 changes: 39 additions & 3 deletions src/Web/Grand.Web.Admin/Services/CourseViewModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public partial class CourseViewModelService : ICourseViewModelService
private readonly ITranslationService _translationService;
private readonly ICustomerActivityService _customerActivityService;
private readonly IProductCourseService _productCourseService;
private readonly IDownloadService _downloadService;
private readonly IServiceProvider _serviceProvider;
private readonly SeoSettings _seoSettings;

public CourseViewModelService(ICourseService courseService, ICourseLevelService courseLevelService, ICourseLessonService courseLessonService,
ICourseSubjectService courseSubjectService,
ISlugService slugService, IPictureService pictureService, ILanguageService languageService,
ITranslationService translationService, ICustomerActivityService customerActivityService, IProductCourseService productCourseService,
IDownloadService downloadService,
IServiceProvider serviceProvider,
SeoSettings seoSettings)
{
Expand All @@ -58,6 +60,7 @@ public CourseViewModelService(ICourseService courseService, ICourseLevelService
_translationService = translationService;
_customerActivityService = customerActivityService;
_productCourseService = productCourseService;
_downloadService = downloadService;
_serviceProvider = serviceProvider;
_seoSettings = seoSettings;
}
Expand All @@ -72,7 +75,7 @@ public virtual async Task<CourseModel> PrepareCourseModel(CourseModel model = nu

foreach (var item in await _courseLevelService.GetAll())
{
model.AvailableLevels.Add(new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem()
model.AvailableLevels.Add(new SelectListItem()
{
Text = item.Name,
Value = item.Id
Expand Down Expand Up @@ -147,7 +150,6 @@ public virtual async Task<Course> UpdateCourseModel(Course course, CourseModel m
if (!string.IsNullOrEmpty(course.ProductId))
await _productCourseService.UpdateCourseOnProduct(course.ProductId, course.Id);


//activity log
await _customerActivityService.InsertActivity("EditCourse", course.Id, _translationService.GetResource("ActivityLog.EditCourse"), course.Name);

Expand All @@ -171,7 +173,7 @@ public virtual async Task<CourseLessonModel> PrepareCourseLessonModel(string cou

foreach (var item in await _courseSubjectService.GetByCourseId(courseId))
{
model.AvailableSubjects.Add(new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem()
model.AvailableSubjects.Add(new SelectListItem()
{
Text = item.Name,
Value = item.Id
Expand All @@ -192,6 +194,9 @@ public virtual async Task<CourseLesson> InsertCourseLessonModel(CourseLessonMode

public virtual async Task<CourseLesson> UpdateCourseLessonModel(CourseLesson lesson, CourseLessonModel model)
{
var prevAttachmentId = lesson.AttachmentId;
var prevVideoFile = lesson.VideoFile;

string prevPictureId = lesson.PictureId;
lesson = model.ToEntity(lesson);
await _courseLessonService.Update(lesson);
Expand All @@ -204,6 +209,22 @@ public virtual async Task<CourseLesson> UpdateCourseLessonModel(CourseLesson les
await _pictureService.DeletePicture(prevPicture);
}

//delete an old "attachment" file (if deleted or updated)
if (!string.IsNullOrEmpty(prevAttachmentId) && prevAttachmentId != lesson.AttachmentId)
{
var prevAttachment = await _downloadService.GetDownloadById(prevAttachmentId);
if (prevAttachment != null)
await _downloadService.DeleteDownload(prevAttachment);
}

//delete an old "video" file (if deleted or updated)
if (!string.IsNullOrEmpty(prevVideoFile) && prevVideoFile != lesson.VideoFile)
{
var prevVideo = await _downloadService.GetDownloadById(prevVideoFile);
if (prevVideo != null)
await _downloadService.DeleteDownload(prevVideo);
}

//activity log
await _customerActivityService.InsertActivity("EditCourseLesson", lesson.Id, _translationService.GetResource("ActivityLog.EditLessonCourse"), lesson.Name);

Expand All @@ -212,6 +233,21 @@ public virtual async Task<CourseLesson> UpdateCourseLessonModel(CourseLesson les
public virtual async Task DeleteCourseLesson(CourseLesson lesson)
{
await _courseLessonService.Delete(lesson);

if (!string.IsNullOrEmpty(lesson.VideoFile))
{
var prevVideo = await _downloadService.GetDownloadById(lesson.VideoFile);
if (prevVideo != null)
await _downloadService.DeleteDownload(prevVideo);
}

if (!string.IsNullOrEmpty(lesson.AttachmentId))
{
var prevAttachment = await _downloadService.GetDownloadById(lesson.AttachmentId);
if (prevAttachment != null)
await _downloadService.DeleteDownload(prevAttachment);
}

//activity log
await _customerActivityService.InsertActivity("DeleteCourseLesson", lesson.Id, _translationService.GetResource("ActivityLog.DeleteCourseLesson"), lesson.Name);
}
Expand Down
11 changes: 11 additions & 0 deletions src/Web/Grand.Web.Admin/Services/CustomerViewModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public partial class CustomerViewModelService : ICustomerViewModelService
private readonly IProductService _productService;
private readonly ISalesEmployeeService _salesEmployeeService;
private readonly ICustomerNoteService _customerNoteService;
private readonly IDownloadService _downloadService;
private readonly IServiceProvider _serviceProvider;

private readonly TaxSettings _taxSettings;
Expand Down Expand Up @@ -102,6 +103,7 @@ public CustomerViewModelService(
IProductService productService,
ISalesEmployeeService salesEmployeeService,
ICustomerNoteService customerNoteService,
IDownloadService downloadService,
IServiceProvider serviceProvider,
CustomerSettings customerSettings,
TaxSettings taxSettings,
Expand Down Expand Up @@ -137,6 +139,7 @@ public CustomerViewModelService(
_productService = productService;
_salesEmployeeService = salesEmployeeService;
_customerNoteService = customerNoteService;
_downloadService = downloadService;
_serviceProvider = serviceProvider;
}

Expand Down Expand Up @@ -1525,6 +1528,14 @@ public virtual async Task DeleteCustomerNote(string id, string customerId)
throw new ArgumentException("No customer note found with the specified id");

await _customerNoteService.DeleteCustomerNote(customerNote);

//delete an old "attachment" file
if (!string.IsNullOrEmpty(customerNote.DownloadId))
{
var attachment = await _downloadService.GetDownloadById(customerNote.DownloadId);
if (attachment != null)
await _downloadService.DeleteDownload(attachment);
}
}
}
}
Loading

0 comments on commit 5afe95e

Please sign in to comment.