/// <summary> /// 根据配置文件生成缩略图 /// </summary> /// <param name="cPath">绝对路径</param> /// <param name="categoryType"></param> public void MakeThumbnail(string cPath, FileTypesEnum categoryType) { #region 生成缩略图 string[] imgFormat = new[] { "JPEG", "JPG", "PNG", "BMP" }; //如果是图片才会进行裁切操作 //缩略图保存格式,是根据配置文件ImagesDPI.xml里面的dpi来命名 if (imgFormat.Any(o => System.IO.Path.GetExtension(cPath).ToUpper().Contains(o))) { string strCategoryType = categoryType.ToString(); var imageDpi = ImagesDpiConfigManager.Value.GetImagesDpiConfigByCategoryType(strCategoryType); if (imageDpi != null) { ImagesDpiConfigManager.Value.MakeThumbnail(cPath, imageDpi);//生成的缩略图的路径格式是:原图路径 + “-”+ dpi + 后缀 } } #endregion }
/// <summary> /// 保存多文件上传 /// </summary> /// <param name="currentUserId">当前用户对象</param> /// <param name="modelId">关联的其他表Id</param> /// <param name="fileType">文件类型枚举</param> /// <param name="fileUpload">上传文件路径</param> /// <param name="fileIds">文件Id(如果是修改就可能有Id),可空</param> /// <param name="videoInfoList">视频的详情</param> /// <returns></returns> public List <Files> SaveMultUploadFile(long currentUserId, string modelId, FileTypesEnum fileType, List <string> fileUpload, List <int> fileIds, List <string> videoInfoList = null) { var oldFiles = _fileAppService.GetFilesListByCategoryIdAndType(modelId.ToString(), fileType.ToString()); bool canCommit = false; List <Files> deleteFileList = new List <Files>();//删除的图片 for (int i = oldFiles.Count - 1; i >= 0; i--) { if (fileIds == null || fileIds.Contains(oldFiles[i].Id) == false) { _fileAppService.Delete(oldFiles[i]); deleteFileList.Add(oldFiles[i]);//记录删除的图片 oldFiles.RemoveAt(i); canCommit = true; } } if (fileUpload.IsNotNull() && fileUpload.Any()) { var files = oldFiles.Select(o => o.Url).ToArray(); for (int i = 0; i < fileUpload.Count; i++) { var o = fileUpload[i]; if (files.Contains(o.Trim()) == false) { var f = BindUploadFiles(o, modelId.ToString(), fileType, currentUserId, false); if (f.IsNotNull()) { fileUpload[i] = f.Url; if (videoInfoList.IsNotEmpty()) { f.Description = videoInfoList[i]; } oldFiles.Add(f); } } } canCommit = true; } if (canCommit) { if (oldFiles.Any()) { int i = 1; foreach (var o in oldFiles) { o.SortNo = i++; } _fileAppService.Update(oldFiles); } foreach (var item in deleteFileList)//移动删除的文件 { MoveToRecycleAsync(item.Url); } } return(oldFiles); }
/// <summary> /// 关联文件到对应的数据,并移动到指定的路径,并保存数据到数据库返回File对象 /// </summary> /// <param name="interimPath"></param> /// <param name="categoryId"></param> /// <param name="categoryType"></param> /// <param name="currentUserId"></param> /// <param name="isReplace">是否替换掉之前所有相同类型(categoryType)的文件</param> /// <param name="customSaveDirPath"></param> /// <returns></returns> public Files BindUploadFiles(string interimPath, string categoryId, FileTypesEnum categoryType, long currentUserId, bool isReplace = true, string customSaveDirPath = "") { string interimFullPath = WebHelper.GetMapPath(interimPath); System.IO.FileInfo fi = new System.IO.FileInfo(interimFullPath); if (fi.Exists) { var fileHandler = DealWithUploadFileHelper.GetInstance(); try { string fileSourceName = string.Empty; //文件名 string fileName = string.Empty; //文件路径 //当包含]时为新传文件 //var f = fi.Name.IndexOf("]") > -1; if (fi.Name.IndexOf(']') < 1) { return(null); } int index = fi.Name.LastIndexOf(']'); fileName = fi.Name.Substring(index + 1); fileSourceName = fi.Name.Substring(0, index).TrimStart('['); string oldPath = string.Empty; bool isCreate = false; Files model = null; if (isReplace && categoryId.IsNotNullAndNotEmpty()) { model = _fileAppService.GetFilesByCategoryIdAndType(categoryId, categoryType.ToString()); if (model != null) { if (model.Url == interimPath) { return(null); } model.SortNo = 0; oldPath = model.OldUrl; model.OldUrl = model.Url; } } if (model == null) { isCreate = true; model = new Files(); //model.Id = IdentityCreator.NewGuid; model.CreationTime = DateTime.Now; model.RelateId = categoryId; model.CategoryType = categoryType.ToString(); model.OldUrl = ""; model.SortNo = 0; model.CreatorUserId = currentUserId; model.TenantId = 1; } model.FileName = fileSourceName.Substring(0, fileSourceName.Length > 100 ? 100 : fileSourceName.Length); model.FileSize = (int)fi.Length; string cPath = string.Empty; if (customSaveDirPath.IsNullOrEmpty()) { string baseDirPath = DealWithUploadFileHelper.GetInstance().GetFilesSaveDirPath(categoryType.ToString());//保存文件夹路径,路径是从根目录开始 cPath = baseDirPath; } else { cPath = customSaveDirPath; } model.Url = System.IO.Path.Combine(cPath, fileName); cPath = WebHelper.GetMapPath(cPath);//保存文件夹完整路径 if (!System.IO.Directory.Exists(cPath)) { System.IO.Directory.CreateDirectory(cPath); } if (isCreate) { model = _fileAppService.Insert(model); } else { model = _fileAppService.Update(model); } cPath = WebHelper.GetMapPath(model.Url); if (!System.IO.File.Exists(cPath)) { fi.MoveTo(cPath); } #region 视频的封面处理 string videoCoverImg = string.Empty; if (System.IO.Path.GetExtension(fi.FullName).ToUpper().Contains("MP4")) { string dir = System.IO.Path.GetDirectoryName(interimFullPath); string videoFileName = System.IO.Path.GetFileNameWithoutExtension(cPath) + ".jpg"; string coverSourceImgPath = string.Concat(dir, "\\", videoFileName); if (System.IO.File.Exists(coverSourceImgPath)) { string saveCoverPath = string.Concat(System.IO.Path.GetDirectoryName(cPath), "\\", videoFileName); new System.IO.FileInfo(coverSourceImgPath).MoveTo(saveCoverPath); MakeThumbnail(saveCoverPath, categoryType);//封面裁切成指定尺寸 } } #endregion #region 处理上传图片,需要缩略图等多种尺寸 #region 生成缩略图 MakeThumbnail(cPath, categoryType); #endregion #endregion //更新删除旧文件 if (oldPath.IsNotNullAndNotEmpty()) { MoveToRecycleAsync(oldPath); } return(model); } catch (Exception ex) { throw ex; } finally { } } return(null); }