/// <summary>生成应用数据</summary> private AppDataSaveRequest BuildAppData(SubmissionProduct product, Metadata indexData) { var appData = new AppDataSaveRequest(); appData.GenerateThumbnail = false; appData.GenerateFulltext = false; var fileName = Path.GetFileName(product.File); appData.Name = fileName; var indexShowType = indexData.ShowType.ToEnum <IndexShowType>(true); switch (indexShowType) { case IndexShowType.Html: appData.ContentType = IndexAppContentType.Html; break; case IndexShowType.Table: case IndexShowType.PropertyGrid: appData.ContentType = IndexAppContentType.Json; if (ExcelUtil.Support(product.Ext)) { appData.Content = new object[] { ExcelBuilder.BuildTable(product) }; } break; case IndexShowType.Chart: appData.ContentType = IndexAppContentType.Json; if (ExcelUtil.Support(product.Ext)) { appData.Content = ExcelBuilder.BuildChart(product.File); } break; default: appData.ContentType = IndexAppContentType.File; appData.StorageType = FileStorageType.FileSystem; appData.IsOnline = true; if (indexData.Thumbnail.IsNullOrEmpty()) { appData.GenerateThumbnail = true; } if (indexData.Fulltext.IsNullOrEmpty()) { appData.GenerateFulltext = true; } break; } appData.Uploader = this.Config.ApiService.UserName; appData.System = indexData.System; appData.ResourceType = indexData.ResourceType; appData.ResourceKey = indexData.ResourceKey; return(appData); }
/// <summary>保存</summary> public async Task <AppDataSaveResult> SaveAsync(AppDataSaveRequest request) { var pair = await Validate(request, true); var doc = pair.Key; var filter = Builders <TDocument> .Filter.Eq(e => e.Id, doc.Id); var options = new UpdateOptions { IsUpsert = true }; await Accessor.ReplaceOneAsync(filter, doc, options); return(pair.Value); }
/// <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 }
/// <summary>保存</summary> public AppDataSaveResult Save(AppDataSaveRequest request) { return(Task.Run(() => SaveAsync(request)).Result); }
/// <summary>保存</summary> private async Task <AppDataSaveResult> SaveAsyncInternal(HttpClientWrapper client, AppDataSaveRequest request) { return(await client.PostObjectAsync <AppDataSaveResult>(GetActionUrl(nameof(Save)), request).ConfigureAwait(false)); }
/// <summary>保存</summary> public async Task <AppDataSaveResult> SaveAsync(AppDataSaveRequest request) { var client = InitHttpClient(); return(await SaveAsyncInternal(client, request).ConfigureAwait(false)); }
/// <summary>保存</summary> public AppDataSaveResult Save(AppDataSaveRequest request) { var client = InitHttpClient(); return(Task.Run(() => SaveAsyncInternal(client, request)).Result); }