コード例 #1
0
ファイル: Helper.cs プロジェクト: Kahaif/SimpleVersioning
 public static async Task DeleteAllFiles(this IStorageRepositoryAsync storageRepo)
 {
     await foreach (var file in storageRepo.GetFilesAsync())
     {
         await storageRepo.DeleteAsync <File>(file.Id);
     }
 }
コード例 #2
0
        public async Task <Task> CleanupDatabase(CancellationToken cancellationToken = default)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();

            var items = await _storageRepository.GetAllAsync();

            _logger.LogInformation("Storage Agent: Database Cleanup operation running...");
            _logger.LogInformation(" found {total} items in storage database", items.Count);
            foreach (var item in items)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    _logger.LogInformation("Storage Agent: Database Cleanup operation Aborted due to trigger cancellation.");
                    //break;
                    return(Task.FromCanceled(cancellationToken));
                }
                if (File.Exists(item.Url) is false)
                {
                    _logger.LogWarning("Storage Agent: Database Cleanup found an item {item} in database, but related file path {fileUrl} is not exist in Storage FileSystem, Trying to delete record.", item.Name, item.Url);
                    await _storageRepository.DeleteAsync(item);

                    _logger.LogWarning("Record ID {Id} with name {name} Deleted Successfully from database.", item.Id, item.Name);
                }
            }
            watch.Stop();
            _logger.LogInformation("Storage Agent: Dtabase Cleanup operation finished after {elapsed} ms.", watch.ElapsedMilliseconds);
            //cancellationToken.ThrowIfCancellationRequested();
            return(Task.CompletedTask);
        }
        public async Task <Response <int> > Handle(DeleteContentByIdCommand command, CancellationToken cancellationToken)
        {
            Item content = await _storageRepository.GetByIdAsync(command.Id);

            if (content is null)
            {
                throw new ApiException(_configuration["Storage:Messages:NotFound"], command.Id);
                //return null;
            }
            Response <int> storageResult = _storageFileSystemProvider.DeleteAsync(content.Url, cancellationToken);

            if (storageResult.Succeeded is true)
            {
                await _storageRepository.DeleteAsync(content);
            }

            storageResult.Data = command.Id;
            return(storageResult);
        }