Esempio n. 1
0
        public async Task <IActionResult> Upload(IFormFile file)
        {
            var uploadRequest = new BlobUploadRequest()
            {
                ContentType = file.ContentType,
                Name        = Guid.NewGuid().ToString()
            };

            if (file.Length > 0)
            {
                using (var ms = new MemoryStream())
                {
                    file.CopyTo(ms);
                    uploadRequest.Bytes = ms.ToArray();
                }
            }
            else
            {
                return(BadRequest("Empty file"));
            }

            var upn = User.Claims.FirstOrDefault(x => x.Type == Consts.Claims.Upn)?.Value;

            // Get paths for storage
            var paths = _storageAccess.GetPathForPendingImage(uploadRequest.Name);

            // Create image in DB
            int id = await _dataAccess.InsertImage(new Image()
            {
                AddedAt               = DateTime.UtcNow,
                Allowed               = null,
                OwnerUpn              = upn,
                RelativePath          = paths.RelativePath,
                ThumbnailRelativePath = null,
                Uploaded              = false
            });

            // Upload image
            var uploadResponse = await _storageAccess.UploadPendingImage(uploadRequest);

            // Flag image as uploaded in DB
            await _dataAccess.UpdateImageUploadedById(true, id);

            return(RedirectToAction(nameof(ImageDetails), new { id = id }));
        }
Esempio n. 2
0
        public async Task <BlobUploadResponse> UploadPendingImage(BlobUploadRequest req)
        {
            var account    = CloudStorageAccount.Parse(_configuration[Consts.ConnectionStrings.StorageConnectionString]);
            var blobClient = account.CreateCloudBlobClient();
            var container  = blobClient.GetContainerReference(_configuration[Consts.Storage.PendingImagesContainer]);
            await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Blob, new BlobRequestOptions(), new OperationContext());

            var blob = container.GetBlockBlobReference(req.Name);
            await blob.UploadFromByteArrayAsync(req.Bytes, 0, req.Bytes.Length);

            blob.Properties.ContentType = req.ContentType;
            await blob.SetPropertiesAsync();

            return(new BlobUploadResponse()
            {
                Name = blob.Name,
                Url = blob.Uri.ToString(),
                RelativePath = blob.Uri.AbsolutePath
            });
        }