Exemple #1
0
        public async Task <BlobResponseModel> GetReferenceAsync([FromUri] string accountName, [FromUri] string containerName, [FromUri] string name)
        {
            var container = await CheckAccessKey(accountName, containerName);

            var blob = await _blobDbCommand.GetAttributesAsync(container, name);

            if (blob != null)
            {
                return(Mapper.Map <BlobResponseModel>(blob));
            }

            blob = container.CreateBlobAttributes(name);
            blob.Properties.ContentType        = MimeMapping.GetMimeMapping(name);
            blob.Properties.ContentDisposition = "attachment";

            return(Mapper.Map <BlobResponseModel>(blob));
        }
Exemple #2
0
        public async Task <Storage> GetStorageAsync(BlobAttributes blobAttributes, CancellationToken cancellationToken)
        {
            var blobDbCommand = new BlobDbCommand(ConnectionString);

            var target =
                await
                blobDbCommand.GetAttributesAsync(blobAttributes.Id, cancellationToken).ConfigureAwait(false);

            if (target != null && target.Storage != null && target.Storage != Storage.None)
            {
                return(target.Storage);
            }

            var account = blobAttributes.Container.Account;

            if (account.StorageType == StorageType.Common)
            {
                return(await GetCommonStorageAsync(blobAttributes, cancellationToken).ConfigureAwait(false));
            }

            if (!account.Storages.Any() || account.Storages.All(x => x.IsSuspended || x.ThresholdLength == 0))
            {
                return(Storage.None);
            }

            var storage = account.Storages.OrderBy(x =>
            {
                var threshold   = x.ThresholdLength * 1.0;
                var used        = x.UsedLength;
                var thisTimeUse = blobAttributes.Properties.Length;

                return((used + thisTimeUse) / threshold);
            }).First();

            return(storage);
        }
Exemple #3
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"));
        }