Exemple #1
0
        public async Task <HttpResponseMessage> SaveAsync([FromUri] string accountName, [FromUri] string containerName)
        {
            if (Request.Content.IsMimeMultipartContent() == false)
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            var container = await CheckAccessKey(accountName, containerName);

            var provider = await Request.Content.ReadAsMultipartAsync();

            var attributeContent = provider.Contents.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "attributes");

            if (attributeContent == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            var blobRequest = await attributeContent.ReadAsAsync <BlobRequestModel>();

            blobRequest.Properties.LastModifiedUtcDateTime = DateTime.UtcNow;
            var blobAttributes = container.CreateBlobAttributes(blobRequest.Name);

            Mapper.Map(blobRequest, blobAttributes);

            var fileContent = provider.Contents.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "file");

            if (fileContent == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var file = await fileContent.ReadAsByteArrayAsync();

            var blob = new Blob(blobAttributes)
            {
                Image      = file,
                Properties = { LastModifiedUtcDateTime = DateTime.UtcNow },
            };

            await _blobDbCommand.SaveAsync(blob);

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Exemple #2
0
        public async Task <ActionResult> UploadImage(string accountName, string containerName)
        {
            ViewBag.AccountName   = accountName;
            ViewBag.ContainerName = containerName;

            if (Request.Files.Count == 0)
            {
                return(View("Upload"));
            }
            var file = Request.Files[0];

            if (file == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (string.IsNullOrWhiteSpace(file.FileName))
            {
                return(View("Upload"));
            }

            var account = await _accountDbCommand.FindByNameAsync(accountName);

            if (account == null)
            {
                return(HttpNotFound());
            }
            if (account.Owner != User.Identity.Name)
            {
                return(new HttpUnauthorizedResult());
            }

            var container = await _containerDbCommand.FindAsync(account, containerName);

            if (container == null)
            {
                return(HttpNotFound());
            }

            var image       = new byte[file.ContentLength];
            var fileName    = new FileInfo(file.FileName).Name;
            var contentType = MimeMapping.GetMimeMapping(fileName);
            await file.InputStream.ReadAsync(image, 0, file.ContentLength);

            var attributes = await _blobDbCommand.GetAttributesAsync(container, fileName);

            Blob blob;

            if (attributes != null)
            {
                blob = new Blob(attributes)
                {
                    Image      = image,
                    Properties =
                    {
                        ContentType             = contentType,
                        LastModifiedUtcDateTime = DateTime.UtcNow,
                        Length                  = file.ContentLength
                    },
                };
            }
            else
            {
                blob = new Blob(container.CreateBlobAttributes(fileName))
                {
                    Image      = image,
                    Properties =
                    {
                        ContentType             = contentType,
                        LastModifiedUtcDateTime = DateTime.UtcNow,
                        Length                  = file.ContentLength
                    },
                };
            }

            await _blobDbCommand.SaveAsync(blob);

            return(RedirectToAction("Index"));
        }