/// <summary>
        /// 处理上传文件的格式
        /// </summary>
        /// <param name="fileModel"></param>
        private void FormatFile(TraficFiles fileModel)
        {
            if (FileHelper.IsOfficeWordDocument(fileModel.FilePath) || FileHelper.IsPdfDocument(fileModel.FilePath))
            {
                fileModel = FileProcesser.Format(fileModel);

                var fileBll = new TraficFilesBll();
                fileBll.Update(fileModel);

                SearchHelper.AddSearchTask(1, fileModel.Id);
            }
        }
Exemple #2
0
        /// <summary>
        /// 将上传的文件进行格式转换处理,若文件格式符合处理条件,则在文件处理完成之后会将处理后得到的新文件地址赋值给 fileModel 的 FilePath 字段并返回
        /// </summary>
        /// <param name="fileModel"><see cref="TraficFiles"/>类的实体对象</param>
        public static TraficFiles Format(TraficFiles fileModel)
        {
            try
            {
                var fileName      = PathExtension.MapPath(fileModel.FilePath);
                var path          = Path.GetDirectoryName(fileName);
                var htmlFilesPath = Path.Combine(path, Guid.NewGuid().ToString());

                var ext = Path.GetExtension(fileName).ToLower();
                switch (ext)
                {
                case ".doc":
                case ".docx":
                    FileHelper.Word2Html(fileName, htmlFilesPath);
                    break;

                //case ".pdf":
                //    FileHelper.Pdf2Html(fileName, htmlFilesPath);
                //    break;

                default:
                    //throw new NotImplementedException();
                    return(fileModel);
                }

                // 将 __html 中的文件打包压缩成 zip 格式的文件
                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
                var zipFileName = Path.Combine(path, fileNameWithoutExtension + ".zip");
                FileHelper.Zip(zipFileName, htmlFilesPath);

                // 删除 __html 目录
                Directory.Delete(htmlFilesPath, true);

                // 将 zip 文件路径处理成相对路径
                fileName = fileName.Replace("\\", "/");
                var webDir = fileName.Replace(fileModel.FilePath, "");
                fileModel.FilePath = zipFileName.Replace("\\", "/").Replace(webDir, "");
                fileModel.FilePath = fileModel.FilePath.StartsWith("/") ? fileModel.FilePath : $"/{fileModel.FilePath}";

                var n = fileModel.FileName;
                fileModel.FileExtension = ".zip";
                fileModel.FileName      = Path.GetFileNameWithoutExtension(n) + fileModel.FileExtension;
                return(fileModel);
            }
            catch (Exception ex)
            {
                ExceptionLogBll.ExceptionPersistence(nameof(FileProcesser), nameof(FileProcesser), ex);
                return(fileModel);
            }
        }
        public JsonResult AddFiles()
        {
            var typeId   = Request["typeId"].ToInt32();
            var departId = Request["depart"].ToInt32();

            if (typeId == 0 || departId == 0)
            {
                return(Json(new { msg = ErrorModel.InputError }));
            }

            // 将文件保存到本地
            var uploadRes    = UploadHelper.FileUpload();
            var filePathInfo = uploadRes as FilePathInfo;

            if (filePathInfo == null)
            {
                return(Json(new { msg = uploadRes }));
            }

            var fileBll = new TraficFilesBll();

            // 验证文件是否重名
            var condition = $"IsDelete=0 AND TypeId={typeId} AND FileName='{filePathInfo.OriginalFileName}'";

            if (fileBll.Exists(condition))
            {
                return(Json(ErrorModel.FileExists));
            }

            var loginUser = LoginStatus.GetLoginId();
            var fileModel = new TraficFiles
            {
                FileExtension  = filePathInfo.FileExtension,
                FileName       = filePathInfo.OriginalFileName,
                FilePath       = filePathInfo.FileRelativePath,
                OriginFilePath = filePathInfo.FileRelativePath,
                FileSize       = filePathInfo.FileSize,
                TypeId         = typeId,
                CreatorId      = loginUser,
                DepartmentId   = departId
            };

            var logBll = new OperateLogBll();
            var dirBll = new TraficFileTypeBll();
            var dir    = dirBll.QuerySingle(typeId);
            var log    = $"在目录[{dir.TypeName}]下添加了文件《{filePathInfo.OriginalFileName}》";

            // 执行插入操作、数据库更新日志插入操作以及后台操作日志插入操作
            var success = fileBll.ExecuteTransation(
                () => fileBll.Insert(fileModel).Id > 0,
                () => DataUpdateLog.SingleUpdate(nameof(TraficFiles), fileModel.Id, DataUpdateType.Insert),
                () => logBll.Add(nameof(TraficFiles), fileModel.Id, DataUpdateType.Insert, loginUser, log)
                );

            if (success)
            {
                // 开启新线程执行将 office 文件转换为 html 的任务
                // 以避免阻塞网络请求线程,造成用户长时间的等待
                Task.Factory.StartNew(() => FormatFile(fileModel));

                return(Json(new
                {
                    msg = ErrorModel.OperateSuccess,
                    data = uploadRes,
                    fileId = fileModel.Id,
                    fileModel
                }));
            }

            return(Json(new { msg = ErrorModel.OperateFailed }));
        }