/// <summary>结束更新文档</summary> private async Task EndUpdate(bool success, FileConvertType type, TDocument newFileDoc, TDocument uploadFileDoc) { var filter = Builders <TDocument> .Filter.Eq(f => f.FileId, uploadFileDoc.FileId); var updateBuilder = Builders <TDocument> .Update; UpdateDefinition <TDocument> update = null; var updateFileId = success ? newFileDoc.FileId : uploadFileDoc.FileId; switch (type) { case FileConvertType.Pdf: update = updateBuilder.Set(u => u.PdfFileId, updateFileId); break; case FileConvertType.Image: update = updateBuilder.Set(u => u.ImageFileId, updateFileId); break; case FileConvertType.Thumbnail: update = updateBuilder.Set(u => u.ThumbnailFileId, updateFileId); break; case FileConvertType.FullText: update = updateBuilder.Set(u => u.FullTextFileId, updateFileId); break; case FileConvertType.Html: update = updateBuilder.Set(u => u.HtmlFileId, updateFileId); break; } await this.UploadFilesCollection.UpdateOneAsync(filter, update); switch (type) { case FileConvertType.Pdf: uploadFileDoc.PdfFileId = updateFileId; break; case FileConvertType.Image: uploadFileDoc.ImageFileId = updateFileId; break; case FileConvertType.Thumbnail: uploadFileDoc.ThumbnailFileId = updateFileId; break; case FileConvertType.FullText: uploadFileDoc.FullTextFileId = updateFileId; break; case FileConvertType.Html: uploadFileDoc.HtmlFileId = updateFileId; break; } }
/// <summary>结束生成文档</summary> public async Task EndBuild(bool success, FileConvertType type, TDocument newFileDoc, TDocument uploadFileDoc, TDocument normalizedFileDoc) { if (success) { var destFile = this.UploadPath + newFileDoc.RelativePath; newFileDoc.Md5 = BuildMD5(destFile, null); switch (type) { case FileConvertType.Pdf: newFileDoc.PdfFileId = newFileDoc.FileId; break; case FileConvertType.Image: newFileDoc.ImageFileId = newFileDoc.FileId; break; case FileConvertType.Thumbnail: newFileDoc.ThumbnailFileId = newFileDoc.FileId; break; case FileConvertType.FullText: newFileDoc.FullTextFileId = newFileDoc.FileId; break; case FileConvertType.Html: newFileDoc.HtmlFileId = newFileDoc.FileId; break; } await this.UploadFilesCollection.InsertOneAsync(newFileDoc); } await EndUpdate(success, type, newFileDoc, uploadFileDoc); if (normalizedFileDoc != null && normalizedFileDoc != uploadFileDoc) { await EndUpdate(success, type, newFileDoc, normalizedFileDoc); } }
/// <summary>转换编码</summary> private async Task <string> ConvertCharSetAsync(TDocument doc, ServerUploadRequest request) { if (request.CharSet.IsNullOrEmpty()) { return(null); } if (request.FileName.GetMediaTypeFromFile().IsStream()) { return(null); } var utf8 = Encoding.UTF8; if (request.CharSet.Equals(utf8.WebName, StringComparison.OrdinalIgnoreCase)) { return(doc.FileId); } var newFileDoc = BuildNew(null, request.FileName, utf8.WebName); var content = File.ReadAllText(request.ServerFile, Encoding.GetEncoding(request.CharSet)); var newFile = this.UploadPath + newFileDoc.RelativePath; File.WriteAllText(newFile, content, utf8); newFileDoc.Md5 = BuildMD5(newFile, null); newFileDoc.Utf8FileId = newFileDoc.FileId; await this.UploadFilesCollection.InsertOneAsync(newFileDoc); return(newFileDoc.FileId); }
/// <summary>生成新文档</summary> private TDocument BuildNew(string relativePath, string fileName, string charSet) { var doc = new TDocument(); doc.FileId = Guid.NewGuid().ToString(); doc.FileName = fileName; doc.CreateDate = DateTime.Now; var ext = Path.GetExtension(fileName); if (relativePath.IsNullOrEmpty()) { var levelOneDir = doc.CreateDate.ToString("yyyyMM"); var levelTwoDir = doc.CreateDate.ToString("ddHH"); var docPath = $@"{this.UploadPath}\{levelOneDir}\{levelTwoDir}"; if (!Directory.Exists(docPath)) { Directory.CreateDirectory(docPath); } doc.RelativePath = $@"\{levelOneDir}\{levelTwoDir}\{doc.FileId}{ext}"; } else { doc.RelativePath = relativePath; } doc.ContentType = ext.GetMediaType().BuildMediaType(charSet); return(doc); }
/// <summary>在线上传</summary> public async Task OnlineUploadAsync(TDocument doc, string sourceFile) { var relativePath = sourceFile.NormalizeRelativePath(); if (doc.RelativePath.Equals(relativePath, StringComparison.OrdinalIgnoreCase)) { return; } var fullFile = this.UploadPath + relativePath; var fullPath = Path.GetDirectoryName(fullFile); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } else if (File.Exists(fullFile)) { ApiServiceExceptionCodes.FileExists.ThrowUserFriendly("参数无效!", $"{nameof(sourceFile)}[{sourceFile}]指定的文件已经存在!"); } File.Copy(this.UploadPath + doc.RelativePath, this.UploadPath + relativePath, true); var fileName = Path.GetFileName(relativePath); var filter = Builders <TDocument> .Filter .Eq(f => f.FileId, doc.FileId); var update = Builders <TDocument> .Update .Set(u => u.FileName, fileName) .Set(u => u.RelativePath, relativePath); //var options = new UpdateOptions(); await this.UploadFilesCollection.UpdateOneAsync(filter, update); File.Delete(this.UploadPath + doc.RelativePath); doc.FileName = fileName; doc.RelativePath = relativePath; }
/// <summary>插入到Mongo库GridFS中</summary> public async Task <ObjectId> InsertMongoAsync(TDocument doc) { using (var stream = File.OpenRead(this.UploadPath + doc.RelativePath)) { var options = new GridFSUploadOptions(); options.Metadata = new BsonDocument(); options.Metadata[Metadata_UploadFileId] = doc.FileId; return(await this.Bucket.UploadFromStreamAsync(doc.FileName, stream, options)); } }
/// <summary>检查编码</summary> private async Task CheckCharSetAsync(bool uploadManyFile, TDocument doc, ServerUploadRequest request) { if (!uploadManyFile) { var fileFormat = request.FileName.GetExtension().GetFileFormat(); if (fileFormat.IsStream) { doc.Utf8FileId = null; return; } } var docContentType = MediaTypeHeaderValue.Parse(doc.ContentType); var newFileDoc = doc; if (request.CharSet.Equals(docContentType.CharSet, StringComparison.OrdinalIgnoreCase)) { if (doc.Utf8FileId != null) { return; } } var utf8 = Encoding.UTF8; if (!utf8.WebName.Equals(request.CharSet, StringComparison.OrdinalIgnoreCase)) { newFileDoc = BuildNew(null, doc.FileName, utf8.WebName); var file = this.UploadPath + doc.RelativePath; var content = File.ReadAllText(file, Encoding.GetEncoding(request.CharSet)); var newFile = this.UploadPath + newFileDoc.RelativePath; File.WriteAllText(newFile, content, utf8); newFileDoc.Utf8FileId = newFileDoc.FileId; newFileDoc.Md5 = BuildMD5(newFile, null); await this.UploadFilesCollection.InsertOneAsync(newFileDoc); } docContentType.CharSet = request.CharSet; var newContentType = docContentType.ToString(); var filter = Builders <TDocument> .Filter.Eq(f => f.FileId, doc.FileId); var update = Builders <TDocument> .Update .Set(u => u.Utf8FileId, newFileDoc.FileId) .Set(u => u.ContentType, newContentType); await this.UploadFilesCollection.UpdateOneAsync(filter, update); doc.Utf8FileId = newFileDoc.FileId; doc.ContentType = newContentType; }