private async Task <MediaOperationCompletedMessage> DeleteAsync( Guid id, CancellationToken cancellationToken) { MediaOperationCompletedMessage msg = new MediaOperationCompletedMessage { Type = MediaOperationType.Delete, MediaId = id, }; Media media = await _mediaService.GetByIdAsync(id, cancellationToken); try { await _faceService.DeleteByMediaIdAsync(media.Id, cancellationToken); await _mediaService.DeleteAsync(media, cancellationToken); msg.Message = $"{media.Filename} deleted"; msg.IsSuccess = true; } catch (Exception ex) { msg.IsSuccess = false; msg.Message = ex.Message; } return(msg); }
public async Task CleanUpDeletedAsync(CancellationToken cancellationToken) { FilterDefinition <Media> filter = Builders <Media> .Filter.Regex( x => x.Folder, new BsonRegularExpression("^Deleted", "i")); IAsyncCursor <Media> cursor = await _context.Medias .FindAsync(filter, null, cancellationToken); List <Media> items = await cursor.ToListAsync(cancellationToken); int totalCount = items.Count; int completedCount = 0; int errorCount = 0; Log.Information("{Count} items found", totalCount); foreach (Media media in items) { try { Log.Information("{Completed} of {Count} - {Id}", completedCount, totalCount, media.Id); IEnumerable <MediaFileInfo> files = _mediaService.GetMediaFiles(media); Backup(media, files); if (files.Any(x => x.Type == MediaFileType.Original)) { continue; } await _faceService.DeleteByMediaIdAsync(media.Id, cancellationToken); await _mediaService.DeleteAsync(media, cancellationToken); } catch (Exception ex) { errorCount++; Log.Error(ex, "Error with {Id}", media.Id); } finally { completedCount++; } } }