示例#1
0
        public async Task <IList <AttachmentUpdateIdToken> > SaveAttachmentsMimeCompressAzure(Int32 tenantId, String pathInfo, HttpFileCollectionBase files, Int64 userId, Int64 companyId, Int32 factor, String container)
        {
            if (factor < 0 || factor > 100)
            {
                throw new ArgumentOutOfRangeException(nameof(factor), $"Invalid factor value: {factor}. Expected [0..100]");
            }
            var rm = await RequestModel.CreateFromBaseUrl(_host, Admin, pathInfo);

            ExpandoObject        prms      = new ExpandoObject();
            String               key       = rm.ModelAction.ToPascalCase();
            String               procedure = $"[{rm.schema}].[{rm.model}.{key}.Update]";
            AttachmentUpdateInfo ii        = new AttachmentUpdateInfo
            {
                UserId = userId,
                Id     = rm._id,
                Key    = key
            };

            if (_host.IsMultiTenant)
            {
                ii.TenantId = tenantId;
            }
            if (_host.IsMultiCompany)
            {
                ii.CompanyId = companyId;
            }
            var azureClient = new AzureStorageRestClient();
            var retList     = new List <AttachmentUpdateIdToken>();

            for (Int32 i = 0; i < files.Count; i++)
            {
                HttpPostedFileBase file = files[i];
                var blobName            = $"{Guid.NewGuid()}_{Path.GetFileName(file.FileName)}";

                ii.Mime     = file.ContentType;
                ii.Name     = Path.GetFileName(file.FileName);
                ii.BlobName = $"{container}/{blobName}";
                ii.Stream   = null;

                var stream = CompressImage(file.InputStream, file.ContentType, factor);
                await azureClient.Put(null, container, blobName, stream, (Int32)stream.Length);

                var aout = await _dbContext.ExecuteAndLoadAsync <AttachmentUpdateInfo, AttachmentUpdateOutput>(rm.CurrentSource, procedure, ii);

                retList.Add(new AttachmentUpdateIdToken()
                {
                    Id    = aout.Id,
                    Mime  = file.ContentType,
                    Name  = file.FileName,
                    Token = _tokenProvider.GenerateToken(aout.Token)
                });
            }
            return(retList);
        }
        async Task <Object> SaveFilesAzureStorage(RequestFile ru, ExpandoObject prms, HttpFileCollectionBase files)
        {
            AttachmentUpdateInfo ii = new AttachmentUpdateInfo()
            {
                UserId = prms.Get <Int64>("UserId")
            };

            if (_host.IsMultiTenant)
            {
                ii.TenantId = prms.Get <Int32>("TenantId");
            }
            var resultList  = new List <AttachmentUpdateIdToken>();
            var azureClient = new AzureStorageRestClient();

            for (Int32 i = 0; i < files.Count; i++)
            {
                HttpPostedFileBase file = files[i];
                var blobName            = $"{Guid.NewGuid()}_{Path.GetFileName(file.FileName)}";

                var azureStream = file.InputStream;
                if (ru.imageCompress != null && IsImageForCompress(ru.imageCompress, file))
                {
                    azureStream = CompressImage(file.InputStream, file.ContentType, ru.imageCompress.quality);
                }

                await azureClient.Put(ru.azureSource, ru.container, blobName, azureStream, azureStream.Length);

                ii.Name     = Path.GetFileName(file.FileName);
                ii.Mime     = file.ContentType;
                ii.Stream   = null;
                ii.BlobName = $"{ru.container}/{blobName}";
                var result = await _dbContext.ExecuteAndLoadAsync <AttachmentUpdateInfo, AttachmentUpdateOutput>(ru.CurrentSource, ru.FileProcedureUpdate, ii);

                resultList.Add(new AttachmentUpdateIdToken()
                {
                    Id    = result.Id,
                    Name  = ii.Name,
                    Mime  = ii.Mime,
                    Token = result != null ? _tokenProvider.GenerateToken(result.Token) : null
                }
                               );
            }
            return(resultList);
        }