Exemplo n.º 1
0
        /// <summary>验证</summary>
        private async Task <PKSKeyValuePair <TDocument, AppDataSaveResult> > Validate(AppDataSaveRequest request, bool replace)
        {
            #region 检查键
            var          doc    = new TDocument();
            IndexAppData oldDoc = null;
            if (request.DataId.IsNullOrEmpty())
            {
                if (request.ResourceKey.IsNullOrEmpty())
                {
                    ApiServiceExceptionCodes.ResourceKeyNotExists.ThrowUserFriendly("缺少参数!", $"ResourceKey不存在!");
                }
                request.DataId = request.ResourceKey.ToUpperInvariant().ToMD5();
            }
            else
            {
                oldDoc = await Accessor.AsQueryable().FirstAsync(e => e.Id == request.DataId);
            }
            doc.Id       = request.DataId;
            doc.DataId   = request.DataId;
            doc.Name     = request.Name;
            doc.IsOnline = false;
            #endregion
            var context = new UploadFileContext();
            if (request.ContentType != IndexAppContentType.File)
            {
                #region 非文件应用数据
                if (request.Content == null)
                {
                    if (request.UploadFileId.IsNullOrEmpty())
                    {
                        ExceptionCodes.MissingParameterValue.ThrowUserFriendly("缺少参数!", $"Content不存在!");
                    }
                    context.UploadFileDoc = await this.UploadFileHandler.GetAsync(request.UploadFileId);

                    doc.SourceName           = context.UploadFileDoc.FileName;
                    doc.SourceStorageType    = IndexStorageType.Url;
                    doc.SourceContentRef     = context.UploadFileDoc.RelativePath.Replace('\\', '/');
                    context.UploadFileFormat = Path.GetExtension(context.UploadFileDoc.FileName).GetFileFormat();
                }
                switch (request.ContentType)
                {
                case IndexAppContentType.Html:
                    doc.DataType = IndexAppDataType.Html;
                    if (context.UploadFileDoc != null)
                    {
                        if (context.UploadFileFormat.GenerateHtml)
                        {
                            request.Content = await GenerateHtmlAsync(context);
                        }
                        else
                        {
                            request.Content = BuildContent(context);
                        }
                    }
                    break;

                case IndexAppContentType.Json:
                    doc.DataType = IndexAppDataType.Json;
                    if (context.UploadFileDoc != null)
                    {
                        var content = BuildContent(context);
                        request.Content = content.JsonTo();
                    }
                    break;

                default:
                    ExceptionCodes.ParameterParsingFailed.ThrowUserFriendly("参数解析失败!", $"ContentType[{request.ContentType.ToString()}]值无效!");
                    break;
                }
                doc.StorageType = IndexStorageType.Content;
                doc.Content     = JsonUtil.ToObject(request.Content);
                #endregion
            }
            else
            {
                #region 文件应用数据
                if (request.StorageType == FileStorageType.FileSystem && !request.IsOnline)
                {
                    if (request.SourceFile.IsNullOrEmpty())
                    {
                        CopyFromOld(doc, oldDoc);
                    }
                    else
                    {
                        context.UploadFileDoc = await this.UploadFileHandler.OfflineUploadAsync(request.SourceFile);
                        await BuildUrl(doc, context);
                    }
                }
                else
                {
                    if (request.UploadFileId.IsNullOrEmpty())
                    {
                        CopyFromOld(doc, oldDoc);
                    }
                    else
                    {
                        doc.IsOnline          = true;
                        context.UploadFileDoc = await this.UploadFileHandler.GetAsync(request.UploadFileId);

                        if (request.StorageType == FileStorageType.Mongo)
                        {
                            var fileObjectId = await this.UploadFileHandler.InsertMongoAsync(context.UploadFileDoc);

                            doc.SourceStorageType = IndexStorageType.File;
                            doc.SourceContentRef  = fileObjectId.ToString();
                            await BuildUploadFile(doc, context);
                        }
                        else
                        {
                            if (!request.SourceFile.IsNullOrEmpty())
                            {
                                await this.UploadFileHandler.OnlineUploadAsync(context.UploadFileDoc, request.SourceFile);
                            }
                            await BuildUrl(doc, context);
                        }
                    }
                }
                #endregion
            }
            #region 检查属性
            if (!replace || oldDoc == null)
            {
                doc.CreateBy   = request.Uploader;
                doc.CreateDate = DateTime.UtcNow;
            }
            else
            {
                doc.CreateBy   = oldDoc.CreateBy;
                doc.CreateDate = oldDoc.CreateDate;
            }
            doc.LastUpdatedBy   = request.Uploader;
            doc.LastUpdatedDate = DateTime.UtcNow;
            doc.RawAdapter      = request.RawAdapter;
            doc.System          = request.System;
            doc.ResourceType    = request.ResourceType;
            doc.ResourceKey     = request.ResourceKey;
            var pair = new PKSKeyValuePair <TDocument, AppDataSaveResult>();
            pair.Key = doc;
            var result = new AppDataSaveResult();
            result.DataId = doc.DataId;
            if (request.GenerateThumbnail && context.UploadFileDoc != null && context.UploadFileFormat.GenerateThumbnail)
            {
                result.Thumbnail = await GenerateThumbnailAsync(context);
            }
            if (request.GenerateFulltext && context.UploadFileDoc != null && context.UploadFileFormat.GenerateFulltext)
            {
                result.Fulltext = await GenerateFulltextAsync(context);
            }
            pair.Value = result;
            return(pair);

            #endregion
        }
Exemplo n.º 2
0
        /// <summary>根据DataID或FileID获得相关文件流</summary>
        public async Task <DownloadResult> DownloadAsync(DownloadRequest request, IndexAppData doc)
        {
            var    result = new DownloadResult();
            string ext    = null;

            if (doc != null)
            {
                if (request.Source)
                {
                    if (doc.SourceContentRef.IsNullOrEmpty())
                    {
                        ExceptionCodes.ParameterParsingFailed.ThrowUserFriendly("下载失败!", $"DataId[{request.DataId}]不存在源文件!");
                    }
                    request.StorageType = doc.SourceStorageType.Value;
                    request.ContentRef  = doc.SourceContentRef;
                    result.FileName     = doc.SourceName;
                }
                else
                {
                    if (doc.ContentRef.IsNullOrEmpty())
                    {
                        ExceptionCodes.ParameterParsingFailed.ThrowUserFriendly("下载失败!", $"DataId[{request.DataId}]不存在文件!");
                    }
                    request.StorageType = doc.StorageType;
                    request.ContentRef  = doc.ContentRef;
                    ext = Path.GetExtension(doc.Name);
                    if (!ext.IsNullOrEmpty())
                    {
                        result.FileName = doc.Name;
                    }
                }
            }
            if (request.StorageType == IndexStorageType.File)
            {
                var fileObjectId = new ObjectId(request.ContentRef);
                var bucketStream = await Bucket.OpenDownloadStreamAsync(fileObjectId);

                if (result.FileName.IsNullOrEmpty())
                {
                    result.FileName = bucketStream.FileInfo.Filename;
                }
                result.Content = bucketStream;
            }
            else if (request.StorageType == IndexStorageType.Url)
            {
                var relativePath = request.ContentRef.Replace('/', '\\');
                if (result.FileName.IsNullOrEmpty())
                {
                    result.FileName = Path.GetFileName(relativePath);
                }
                result.Content = File.OpenRead(this.UploadPath + relativePath);
            }
            else
            {
                ExceptionCodes.MissingParameterValue.ThrowUserFriendly("下载失败!", $"缺少参数!");
            }
            if (!request.FileName.IsNullOrEmpty())
            {
                ext = Path.GetExtension(request.FileName);
            }
            if (ext.IsNullOrEmpty())
            {
                ext = Path.GetExtension(result.FileName);
            }
            result.ContentType = ext.GetMediaType();
            return(result);
        }