예제 #1
0
        public virtual async Task UploadAsync(FileStreamSM model, string entity, Guid guid, DateTime?date = null)
        {
            if (model is null)
            {
                throw new NullReferenceException($"`Parameter {nameof(model)}` cannot be null!");
            }

            if (string.IsNullOrWhiteSpace(model.FileName))
            {
                throw new ArgumentException(nameof(model.FileName));
            }

            if (model.Stream is null)
            {
                throw new NullReferenceException($"`Parameter {nameof(model.Stream)}` cannot be null!");
            }

            var objectId = await Validate(entity, guid);

            if (date is null)
            {
                date = DateTime.Now;
            }

            var fileReference = new FileReference
            {
                Date     = date.Value,
                Entity   = entity,
                Name     = model.FileName,
                ObjectId = objectId
            };

            var folder = PathToFolderWithFile(entity, date.Value, fileReference.Guid);

            Directory.CreateDirectory(folder);
            var path = PathToFile(folder, fileReference.Name);

            var uploadTask = UploadFileAsync(model.Stream, path);
            var addTask    = ApplicationDbContext.FileReferences.AddAsync(fileReference).AsTask();
            var saveTask   = ApplicationDbContext.SaveChangesAsync();

            try
            {
                await Task.WhenAll(uploadTask, addTask, saveTask);
            }
            catch (NotSupportedImageFormatException ex)
            {
                Logger.LogError(ex,
                                "An error occured in {0} while executing tasks: {1}, {2}",
                                GetFullMemberName(),
                                nameof(uploadTask),
                                nameof(addTask),
                                nameof(saveTask));
                throw;
            }
        }
예제 #2
0
        public virtual async Task OverwriteAsync(FileStreamSM model, Guid guid)
        {
            var fileReference = await ApplicationDbContext.FileReferences.FirstOrDefaultAsync(x => x.Guid == guid);

            if (fileReference is null)
            {
                throw new FileReferenceNotFound(guid);
            }

            if (model is null)
            {
                throw new NullReferenceException($"`Parameter {nameof(model)}` cannot be null!");
            }

            if (string.IsNullOrWhiteSpace(model.FileName))
            {
                throw new ArgumentException(nameof(model.FileName));
            }

            if (model.Stream is null)
            {
                throw new NullReferenceException($"`Parameter {nameof(model.Stream)}` cannot be null!");
            }

            var folder = PathToFolderWithFile(fileReference.Entity, fileReference.Date, fileReference.Guid);

            Directory.Delete(folder, true);
            Directory.CreateDirectory(folder);
            var path = PathToFile(folder, model.FileName);

            var uploadTask = UploadFileAsync(model.Stream, path);

            fileReference.Name = model.FileName;
            ApplicationDbContext.FileReferences.Update(fileReference);
            var saveTask = ApplicationDbContext.SaveChangesAsync();

            try
            {
                await Task.WhenAll(uploadTask, saveTask);
            }
            catch (NotSupportedImageFormatException ex)
            {
                Logger.LogError(ex,
                                "An error occured in {0} while executing tasks: {1}, {2}",
                                GetFullMemberName(),
                                nameof(uploadTask),
                                nameof(saveTask));
                throw;
            }
        }