Esempio n. 1
0
        public async Task <CloudFile> InsertFile(IFormFile asset, string containerName)
        {
            try
            {
                var           member = User.Identity.Name;
                MemberLicense ml     = this.iMemberLicenseRepository.MyLicense(member);

                if (CloudStorageAccount.TryParse(ml?.AzureSaConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                    CloudBlobContainer container = blobClient.GetContainerReference(containerName);

                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(asset.FileName);
                    blockBlob.Metadata.Add("UploadedBy", member);
                    blockBlob.Metadata.Add("CreatedAt", DateTimeOffset.Now.ToString("O")); //2020-01-16T13:25:42.8408729+00:00

                    if (await blockBlob.ExistsAsync())
                    {
                        return(null);
                    }

                    MemberLicenseUsedStorage mlus = this.iMemberLicenseRepository.GetUsedStorage(ml.LicenseId);
                    long newSizeTestInBytes       = long.Parse(BigInteger.Add(mlus.AzureSaUsedSizeInBytes, asset.Length).ToString());
                    long capasityInBytes          = long.Parse(BigInteger.Multiply(ml.AzureSaSizeInGb, 1073741824).ToString());

                    if (newSizeTestInBytes <= capasityInBytes)
                    {
                        await blockBlob.UploadFromStreamAsync(asset.OpenReadStream());

                        // Increase Used Storage Size
                        //await container.FetchAttributesAsync();
                        mlus.AzureSaUsedSizeInBytes += asset.Length;
                        this.iMemberLicenseRepository.UpdateUsedStorage(mlus);
                        // End of Increase Used Storage Size

                        await blockBlob.FetchAttributesAsync();

                        CloudFile newFile = new CloudFile {
                            ContentType = blockBlob.Properties.ContentType, FileName = blockBlob.Name, URL = blockBlob.Uri.ToString(), Size = blockBlob.Properties.Length, UploadedBy = member, CreatedAt = DateTimeOffset.Now, ContainerName = containerName
                        };
                        return(newFile);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }
        }
Esempio n. 2
0
 public bool UpdateUsedStorage(MemberLicenseUsedStorage mlus)
 {
     try
     {
         context.Entry(mlus).State = EntityState.Modified;
         context.SaveChanges();
         return(true);
     }
     catch { }
     return(false);
 }
Esempio n. 3
0
        [HttpGet("{licenseId}/UsedStorage")]                              // GET MemberLicense/UsedStorage
        public IActionResult GetUsedStorage([FromRoute] string licenseId) //Accepts from route parameters not JSON. You don't have to speficy [FromRoute], but you can..
        {
            var member = User.Identity.Name;                              // For security. From Claim(ClaimTypes.Name, Username) in JWT

            MemberLicenseUsedStorage mlus = iMemberLicenseRepository.GetUsedStorage(Guid.Parse(licenseId));

            if (mlus != null)
            {
                return(Ok(mlus));
            }
            return(NotFound());  // 404 resource not found, Microsoft docs use NotFound for this kind of behavior.
        }
Esempio n. 4
0
        public async Task <bool> DeleteFile(string fileName, string containerName)
        {
            try
            {
                var           member = User.Identity.Name;
                MemberLicense ml     = this.iMemberLicenseRepository.MyLicense(member);
                // if (CloudStorageAccount.TryParse(config.Value.StorageConnection, out CloudStorageAccount storageAccount))
                if (CloudStorageAccount.TryParse(ml?.AzureSaConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient    BlobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer container  = BlobClient.GetContainerReference(containerName);

                    if (await container.ExistsAsync())
                    {
                        CloudBlob file = container.GetBlobReference(fileName);

                        if (await file.ExistsAsync())
                        {
                            await file.DeleteAsync();

                            //Decrease used storage
                            MemberLicenseUsedStorage mlus = this.iMemberLicenseRepository.GetUsedStorage(ml.LicenseId);
                            await container.FetchAttributesAsync();

                            // await file.FetchAttributesAsync();

                            mlus.AzureSaUsedSizeInBytes = long.Parse(BigInteger.Subtract(mlus.AzureSaUsedSizeInBytes, file.Properties.Length).ToString());


                            if (mlus.AzureSaUsedSizeInBytes < 0)
                            {
                                mlus.AzureSaUsedSizeInBytes = 0;
                            }

                            this.iMemberLicenseRepository.UpdateUsedStorage(mlus);
                            // End of Decrease Used Storage Size

                            return(true);
                        }
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Esempio n. 5
0
        public ReturnModel NewLicense(MemberLicense memberLicense)
        {
            try
            {
                context.MemberLicense.Add(memberLicense);

                MemberLicenseUsedStorage mlus = new MemberLicenseUsedStorage {
                    LicenseId = memberLicense.LicenseId, AzureSaUsedSizeInBytes = 1
                };
                context.MemberLicenseUsedStorage.Add(mlus);

                context.SaveChanges();
            }
            catch { return(new ReturnModel {
                    ErrorCode = ErrorCodes.DatabaseError
                }); }
            return(new ReturnModel {
                ErrorCode = ErrorCodes.OK, Model = memberLicense.LicenseId
            });                                                                                    // Provides TeamId(identity autoset from db)
        }