public async Task <AttachmentInfo> LoadFileGet(String pathInfo, Action <ExpandoObject> setParams)
        {
            var rm = await RequestModel.CreateFromBaseUrl(_host, Admin, pathInfo);

            var ru = rm.GetFile();

            ExpandoObject loadPrms = new ExpandoObject();

            setParams?.Invoke(loadPrms);
            loadPrms.Set("Id", ru.Id);
            loadPrms.RemoveKeys("export,Export,token,Token");

            switch (ru.type)
            {
            case RequestFileType.sql:
            case RequestFileType.azureBlob:
            {
                var ai = await _dbContext.LoadAsync <AttachmentInfo>(ru.CurrentSource, ru.FileProcedureLoad, loadPrms);

                if (!String.IsNullOrEmpty(ai.BlobName))
                {
                    var azureClient = new AzureStorageRestClient();
                    var blobName    = Path.GetFileName(ai.BlobName);
                    var container   = Path.GetDirectoryName(ai.BlobName);
                    ai.Stream = await azureClient.Get(ru.azureSource, container, blobName);
                }
                return(ai);
            }

            default:
                throw new InvalidOperationException($"Invalid type for file: '{ru.type.ToString()}'");
            }
        }
Example #2
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);
        }
Example #3
0
        public async Task <AttachmentInfo> LoadFileGet(String pathInfo, Action <ExpandoObject> setParams, String token)
        {
            var rm = await RequestModel.CreateFromBaseUrl(_host, pathInfo);

            var ru = rm.GetFile();

            ru.CheckPermissions(_userStateManager?.GetUserPermissions(), _host.IsDebugConfiguration);

            ExpandoObject loadPrms = new ExpandoObject();

            setParams?.Invoke(loadPrms);
            loadPrms.Set("Id", ru.Id);
            loadPrms.RemoveKeys("export,Export,token,Token");

            switch (ru.type)
            {
            case RequestFileType.json:
                var dm = await _dbContext.LoadModelAsync(ru.CurrentSource, ru.LoadProcedure, loadPrms);

                var json = JsonConvert.SerializeObject(dm.Root, JsonHelpers.StandardSerializerSettings);
                return(new AttachmentInfo()
                {
                    SkipToken = true,
                    Mime = MimeTypes.Application.Json,
                    Stream = Encoding.UTF8.GetBytes(json),
                    Name = ru.outputFileName
                });

            case RequestFileType.sql:
            case RequestFileType.azureBlob:
            {
                if (token == null)
                {
                    throw new InvalidOperationException("There is no access token for image");
                }
                var ai = await _dbContext.LoadAsync <AttachmentInfo>(ru.CurrentSource, ru.FileProcedureLoad, loadPrms);

                if (!String.IsNullOrEmpty(ai.BlobName))
                {
                    var azureClient = new AzureStorageRestClient();
                    var blobName    = Path.GetFileName(ai.BlobName);
                    var container   = Path.GetDirectoryName(ai.BlobName);
                    ai.Stream = await azureClient.Get(ru.azureSource, container, blobName);
                }
                return(ai);
            }

            default:
                throw new InvalidOperationException($"Invalid type for file: '{ru.type}'");
            }
        }
        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);
        }
Example #5
0
        public async Task <AttachmentInfo> DownloadAttachment(String pathInfo, Action <ExpandoObject> setParams, String suffix = "Load")
        {
            var rm = await RequestModel.CreateFromBaseUrl(_host, Admin, pathInfo);

            // [{source}].[{schema}].[{base}.{key}.Load]
            ExpandoObject prms = new ExpandoObject();

            setParams?.Invoke(prms);
            String key = rm.ModelAction.ToPascalCase();

            prms.Set("Id", rm._id);
            prms.Set("Key", key);
            String procedure = $"[{rm.schema}].[{rm.model}.{key}.{suffix}]";
            var    ai        = await _dbContext.LoadAsync <AttachmentInfo>(rm.CurrentSource, procedure, prms);

            if (!String.IsNullOrEmpty(ai.BlobName))
            {
                var azureClient = new AzureStorageRestClient();
                ai.Stream = await azureClient.GetBlob(ai.BlobName);
            }
            return(ai);
        }