public async Task Save(Dto.File file)
        {
            var fileId = Guid.NewGuid().ToString("N");

            var zipFileName = $"{fileId}_{file.Name}";

            //keep name length as max 70
            if (zipFileName.Length > 65)
            {
                zipFileName = $"{zipFileName.Substring(0, 66)}.zip";
            }
            else
            {
                zipFileName += ".zip";
            }

            var payload = new
            {
                content = file.Content,
                name    = $"{file.OwnerId}/{zipFileName}"
            };

            var json = JsonConvert.SerializeObject(payload);

            var data = new StringContent(json, Encoding.UTF8, "application/json");

            var result = await Client.PostAsync($"{_apiUrl}/v1/uploads", data);

            result.EnsureSuccessStatusCode();

            file.Id   = fileId;
            file.Name = zipFileName;

            _logger.LogInformation($"{zipFileName} saved successfully");
        }
示例#2
0
        public Task Save(Dto.File file)
        {
            var zipFileName = file.Name;

            //keep name length as max 70
            if (zipFileName.Length > 65)
            {
                zipFileName = $"{zipFileName.Substring(0, 66)}.zip";
            }
            else
            {
                zipFileName += ".zip";
            }

            var entity = new Dal.Entities.File
            {
                Name    = zipFileName,
                OwnerId = file.OwnerId,
                Content = file.Content
            };

            _repository.Insert(entity);

            file.Id   = entity.Id;
            file.Name = zipFileName;

            _logger.LogInformation($"{zipFileName} saved successfully");

            return(Task.CompletedTask);
        }
        private async Task <ApiResponse <string> > Save(UploadFileViewModel model)
        {
            var apiResp = new ApiResponse <string>
            {
                Type = ResponseType.Fail
            };

            if (!_fileService.ValidateSize(model.File.Length))
            {
                apiResp.ErrorCode = ErrorCode.ObjectExceededMaxAllowedLength;
                return(apiResp);
            }

            var(fileName, _) = Utility.GetFileNameAndExtension(model.File.FileName);

            byte[] compressedBytes;

            await using (var outStream = new MemoryStream())
            {
                using (var archive = new ZipArchive(outStream, ZipArchiveMode.Create, true))
                {
                    var fileInArchive = archive.CreateEntry(model.File.FileName, CompressionLevel.Optimal);

                    await using (var entryStream = fileInArchive.Open())
                    {
                        await model.File.CopyToAsync(entryStream);
                    }
                }

                compressedBytes = outStream.ToArray();  // get compressed byte array in order to save content as .zip
            }

            var file = new Dto.File
            {
                OwnerId = GetUserId().Value.ToString(),
                Name    = fileName,
                Content = compressedBytes
            };

            await _fileService.Save(file);

            apiResp.Type = ResponseType.Success;

            apiResp.Data = file.Name;

            return(apiResp);
        }
        public Dto.File GetFile()
        {
            if (file == null)
            {
                file = new Dto.File
                {
                    Parts = new List <Part>
                    {
                        new Part
                        {
                            BlockId = "DR21BDGFL%"
                        },
                        new Part {
                            BlockId = "MQ59AN34"
                        },
                        new Part {
                            BlockId = "2PSO90FNB5"
                        }
                    }
                };
            }

            return(file);
        }
            public CreateFileRecord(FileMetaServiceFixture fileMetaServiceFixture)
            {
                fileMetaService = fileMetaServiceFixture.GetFileMetaService();

                file = fileMetaServiceFixture.GetFile();
            }