Пример #1
0
        /// <summary>
        /// 上传至文件服务器
        /// </summary>
        private async Task <SimplyResult <Guid> > UploadFileServerAsync(byte[] fileBytes, string fileExt = null)
        {
            var fileId = SequentialGuid.NewGuid();

            var file = new
            {
                FileId   = fileId,
                FileName = string.IsNullOrEmpty(fileExt) ? fileId.ToString() : $"{fileId}.{fileExt}",
                Data     = fileBytes
            };

            try
            {
                var httpClient = m_HttpClientFactory.CreateClient("FileClient");
                var response   = await httpClient.PostAsJsonAsync("file/upload", file);

                response.EnsureSuccessStatusCode();

                var responseBody = await response.Content.ReadAsStringAsync();

                var result = JsonConvert.DeserializeObject <WebApiResponse>(responseBody);
                if (!result.Success)
                {
                    throw new Exception(result.Error.Description);
                }

                return(SimplyResult.Ok(fileId));
            }
            catch (Exception ex)
            {
                m_Logger.LogError(ex, "上传文件失败");
                return(SimplyResult.Fail(nameof(Errors.RequestFileAPIFail), Errors.RequestFileAPIFail, Guid.Empty));
            }
        }
Пример #2
0
        public override async Task <SimplyResult <DocConvertResultDto> > GenerateAsync(OSSObjectDto ossObjectDto, bool isRegenerate)
        {
            Requires.NotNull(ossObjectDto, nameof(ossObjectDto));

            var token = await GetTokenAsync(ossObjectDto.Bucket, ossObjectDto.FilePath, ossObjectDto.HashPath, isRegenerate);

            var generateResult = new DocConvertResultDto
            {
                PreviewUrl = GetPreviewUrl(ossObjectDto.Bucket, ossObjectDto.FilePath, token),
                Status     = DocConvertStatus.Finished,
                FileType   = ossObjectDto.FileType
            };

            return(SimplyResult.Ok(generateResult));
        }
Пример #3
0
        public virtual async Task <SimplyResult <DocConvertResultDto> > GenerateAsync(OSSObjectDto ossObjectDto, bool isRegenerate)
        {
            Requires.NotNull(ossObjectDto, nameof(ossObjectDto));

            var generateResult = await m_PreviewRedisService.GetAsync(ossObjectDto.HashPath);

            if ((generateResult?.ETag ?? string.Empty) != ossObjectDto.ETag)
            {
                isRegenerate = true;
            }

            if (isRegenerate)
            {
                generateResult = m_PreviewSetting.IsSyncConvert
                    ? m_IMMService.Convert(ossObjectDto)
                    : m_IMMService.CreateConvertTask(ossObjectDto);
            }

            if (generateResult?.Status == DocConvertStatus.Running && m_PreviewSetting.TaskPollingSpend > 0)
            {
                generateResult = await QueryConvertTaskAsync(generateResult.TaskId);
            }

            switch (generateResult?.Status)
            {
            case DocConvertStatus.Running:
            case DocConvertStatus.Finished:
                generateResult.PreviewUrl = m_IMMSetting.GetPreviewUrl(ossObjectDto.IMMKey);
                generateResult.FileType   = ossObjectDto.FileType;

                await m_PreviewRedisService.SetAsync(ossObjectDto.HashPath, generateResult);

                var token = await GetTokenAsync(m_IMMSetting.Bucket,
                                                $"{m_IMMSetting.GetPrefix(ossObjectDto.IMMKey)}/*", ossObjectDto.HashPath, isRegenerate);

                generateResult.PreviewUrl = GetFullPreviewUrl(generateResult.PreviewUrl, token);
                return(SimplyResult.Ok(generateResult));

            default:
                await m_PreviewRedisService.DeleteAsync(ossObjectDto.HashPath);

                return(SimplyResult.Fail <DocConvertResultDto>("GenerateFail", "文档转换失败"));
            }
        }
Пример #4
0
        public virtual SimplyResult <OSSObjectDto> GetOSSObject(string ossPath)
        {
            Requires.NotNullOrEmpty(ossPath, nameof(ossPath));

            var ossObject = new OSSObjectDto(ossPath);

            var ossObjectMetadata = m_OSSService.GetObjectMetadata(ossObject.Bucket, ossObject.FilePath);

            if (ossObjectMetadata == null)
            {
                return(SimplyResult.Fail <OSSObjectDto>("FileNoExist", "文档不存在"));
            }

            var ossFileType = string.Empty;

            if (!string.IsNullOrEmpty(ossObjectMetadata.ContentDisposition))
            {
                var fileName = Regexs.ContentDispositionFileNameRegex.Match(ossObjectMetadata.ContentDisposition).Value;
                ossFileType = Path.GetExtension(fileName).TrimStart('.');
            }

            if (string.IsNullOrEmpty(ossFileType))
            {
                Resources.ContentTypeDict.TryGetValue(ossObjectMetadata.ContentType.Split(';')[0].ToLower(), out ossFileType);
            }

            if (string.IsNullOrEmpty(ossFileType) || !Resources.IMMFileTypeDict.TryGetValue(ossFileType, out var immFileType))
            {
                return(SimplyResult.Fail <OSSObjectDto>("UnsupportedFile", "不支持的文件类型"));
            }

            ossObject.FileType = immFileType;
            ossObject.ETag     = ossObjectMetadata.ETag;

            return(SimplyResult.Ok(ossObject));
        }