Ejemplo n.º 1
0
        // TODO add uploading by chunks
        // TODO refactor
        public async Task <IActionResult> UpdateBlobAsync(string id, IFormFile file)
        {
            if (file == null)
            {
                return(BadRequest());
            }

            var blob = await _blobStore.GetByIdAsync(id);

            if (blob == null)
            {
                return(NotFound());
            }

            if (FileLengthExceedAllowed(file))
            {
                return(BadRequest());
            }

            var contentType = file.ContentType;
            var fileName    = file.FileName;
            var buffer      = await file.ToByteArrayAsync();

            var subject = await _storageService.UpdateBlobAsync(blob.ContainerId, blob.StorageSubject, buffer);

            if (string.IsNullOrEmpty(subject))
            {
                return(StatusCode(500));
            }

            blob.MimeType       = MimeMapping.GetMimeMapping(fileName);
            blob.SizeInBytes    = buffer.Length;
            blob.StorageSubject = subject;
            blob.OrigFileName   = fileName;

            await _blobStore.UpdateAsync(blob.Id, blob);

            var blobModel = ModelMapper.ToViewModel(blob);

            return(Ok(blobModel));
        }