/// <summary> /// 删除上传文件 /// </summary> /// <param name="fileUri">相对地址或网址</param> public void DeleteFile(string fileUri) { //分发不同的上传方式处理 switch (siteConfig.fileserver) { case "aliyun": //阿里云OSS对象存储 //检查配置是否完善 if (string.IsNullOrEmpty(siteConfig.accessid) || string.IsNullOrEmpty(siteConfig.accesssecret) || string.IsNullOrEmpty(siteConfig.endpoint)) { return; } //初始化配置 DTcms.Cloud.AliyunOss aliyun = new DTcms.Cloud.AliyunOss(siteConfig.endpoint, siteConfig.accessid, siteConfig.accesssecret); string result = string.Empty; //返回信息 aliyun.DeleteObject(siteConfig.spacename, fileUri, siteConfig.spaceurl, out result); break; case "qcloud": //腾讯云COS对象存储 break; default: //本地服务器 //文件不应是上传文件,防止跨目录删除 if (fileUri.IndexOf("..") == -1 && fileUri.ToLower().StartsWith(siteConfig.webpath.ToLower() + siteConfig.filepath.ToLower())) { FileHelper.DeleteUpFile(fileUri); } break; } }
/// <summary> /// 通过文件流上传文件方法 /// </summary> /// <param name="upType">文件上传类型</param> /// <param name="byteData">文件字节数组</param> /// <param name="fileName">文件名</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <param name="isCompress">是否启用图片压缩</param> /// <param name="isCover">是否为封面</param> /// <param name="thumbnailWidth">缩略图宽度</param> /// <param name="thumbnailHeight">缩略图高度</param> /// <param name="thumbnailMode">缩略图生成方式</param> /// <returns>上传成功返回JSON字符串</returns> public string StrFileSaveAs(string upType, byte[] byteData, string fileName, bool isThumbnail, bool isWater, bool isCompress, bool isCover, int thumbnailWidth, int thumbnailHeight, string thumbnailMode) { try { int maxSize = 0; string[] allowExtensions = null; //上传类型 switch (upType.ToLower()) { case "file": maxSize = siteConfig.attachsize; allowExtensions = siteConfig.attachextension.Split(','); break; case "video": //视频上传 maxSize = siteConfig.videosize; allowExtensions = siteConfig.videoextension.Split(','); break; default: maxSize = siteConfig.imgsize; allowExtensions = siteConfig.imgextension.Split(','); break; } string fileExt = Path.GetExtension(fileName).Trim('.').ToLower(); //文件扩展名,不含“.” string newFileName = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名 string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名 string upLoadPath = GetUpLoadPath(); //本地上传目录相对路径 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //本地上传目录的物理路径 string newFilePath = upLoadPath + newFileName; //本地上传后的路径 string newThumbnailPath = upLoadPath + newThumbnailFileName; //本地上传后的缩略图路径 byte[] thumbData = null; //缩略图文件流 //检查文件字节数组是否为NULL if (byteData == null) { return("{\"status\": 0, \"msg\": \"请选择要上传的文件!\"}"); } //检查文件扩展名是否合法 if (!CheckFileExt(fileExt, allowExtensions)) { return("{\"status\": 0, \"msg\": \"不允许上传" + fileExt + "类型的文件!\"}"); } //检查文件大小是否合法 if (!CheckFileSize(byteData.Length, maxSize)) { return("{\"status\": 0, \"msg\": \"文件超过限制的大小!\"}"); } //判断是否为图片存储 if (upType.ToLower() == "img") { //是否是生成封面 if (isCover) { //封面直接裁剪 if (thumbnailWidth > 0 && thumbnailHeight > 0) { byteData = Thumbnail.MakeThumbnailImage(byteData, fileExt, thumbnailWidth, thumbnailHeight, thumbnailMode); } } else { //如果是图片,检查图片是否超出最大尺寸,是则裁剪 if (isCompress && this.siteConfig.imgmaxheight > 0 || this.siteConfig.imgmaxwidth > 0) { byteData = Thumbnail.MakeThumbnailImage(byteData, fileExt, this.siteConfig.imgmaxwidth, this.siteConfig.imgmaxheight); } } //如果是图片,检查是否需要生成缩略图,是则生成 if (isThumbnail && thumbnailWidth > 0 && thumbnailHeight > 0) { thumbData = Thumbnail.MakeThumbnailImage(byteData, fileExt, thumbnailWidth, thumbnailHeight, thumbnailMode); } //else //{ // newThumbnailPath = newFilePath; //不生成缩略图则返回原图 //} //如果是图片,检查是否需要打水印 if (isWater && siteConfig.watermarktype > 0 && IsWaterMark(fileExt)) { switch (this.siteConfig.watermarktype) { case 1: if (!string.IsNullOrEmpty(this.siteConfig.watermarktext)) { byteData = WaterMark.AddImageSignText(byteData, fileExt, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); } break; case 2: if (!string.IsNullOrEmpty(this.siteConfig.watermarkpic)) { byteData = WaterMark.AddImageSignPic(byteData, fileExt, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); } break; } } //图片处理插件选择 if (siteConfig.imgplugin > 0) { //主图 MagickHelper mhr = new MagickHelper(); mhr.byteData = byteData; mhr.quality = 0; if (isCompress) { mhr.quality = siteConfig.watermarkimgquality; } //是否需要转换扩展名 if (!string.IsNullOrEmpty(siteConfig.imgconvert)) { fileExt = siteConfig.imgconvert; mhr.fileExt = siteConfig.imgconvert; //替换扩展名 newFileName = newFileName.Remove(newFileName.LastIndexOf(".") + 1) + fileExt; newThumbnailFileName = newThumbnailFileName.Remove(newThumbnailFileName.LastIndexOf(".") + 1) + fileExt; newFilePath = newFilePath.Remove(newFilePath.LastIndexOf(".") + 1) + fileExt; newThumbnailPath = newThumbnailPath.Remove(newThumbnailPath.LastIndexOf(".") + 1) + fileExt; } byteData = mhr.Compress(); //缩略图 if (thumbData != null) { MagickHelper mhr2 = new MagickHelper(); mhr2.byteData = thumbData; mhr2.quality = mhr.quality < 90 ? 90 : mhr.quality; mhr2.fileExt = mhr.fileExt; } } } else if (upType.ToLower() == "video") { } //分发不同的上传方式处理 switch (siteConfig.fileserver) { case "aliyun": //阿里云OSS对象存储 #region 阿里云OSS对象存储 //检查配置是否完善 if (string.IsNullOrEmpty(siteConfig.accessid) || string.IsNullOrEmpty(siteConfig.accesssecret) || string.IsNullOrEmpty(siteConfig.endpoint)) { return("{\"status\": 0, \"msg\": \"文件上传配置未完善,无法上传\"}"); } //初始化阿里云配置 DTcms.Cloud.AliyunOss aliyun = new DTcms.Cloud.AliyunOss(siteConfig.endpoint, siteConfig.accessid, siteConfig.accesssecret); string result = string.Empty; //返回信息 //保存主文件 if (!aliyun.PutObject(byteData, siteConfig.spacename, newFilePath, siteConfig.spaceurl, out result)) { return("{\"status\": 0, \"msg\": \"" + result + "\"}"); } newFilePath = result; //将地址赋值给新文件地址 //保存缩略图文件 if (thumbData != null) { aliyun.PutObject(thumbData, siteConfig.spacename, newThumbnailPath, siteConfig.spaceurl, out result); newThumbnailPath = result; //将缩略图地址赋值 } else { newThumbnailPath = newFilePath; //没有缩略图将原图返回 } #endregion break; case "qcloud": //腾讯云COS对象存储 #region 腾讯云COS对象存储 #endregion break; case "domain": //本地跨域存储 #region 本地跨域存储 fullUpLoadPath = siteConfig.domainpath + upLoadPath.Replace("/", "\\"); var bindDomain = siteConfig.domainbind; if (bindDomain.EndsWith("/")) { bindDomain = bindDomain.Remove(bindDomain.Length - 1); } newFilePath = bindDomain + newFilePath; //检查本地上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //保存主文件 FileHelper.SaveFile(byteData, fullUpLoadPath + newFileName); //保存缩略图文件 if (thumbData != null) { newThumbnailPath = bindDomain + newThumbnailPath; FileHelper.SaveFile(thumbData, fullUpLoadPath + newThumbnailFileName); } else { newThumbnailPath = newFilePath; } #endregion break; default: //本地服务器 #region 本地服务器 //检查本地上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //保存主文件 FileHelper.SaveFile(byteData, fullUpLoadPath + newFileName); //保存缩略图文件 if (thumbData != null) { FileHelper.SaveFile(thumbData, fullUpLoadPath + newThumbnailFileName); } else { newThumbnailPath = upLoadPath; } #endregion break; } //处理完毕,返回JOSN格式的文件信息 return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \"" + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \"" + newThumbnailPath + "\", \"size\": " + byteData.Length + ", \"ext\": \"" + fileExt + "\"}"); } catch (Exception ex) { LogHelper.Info(ex.Message); return("{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}"); } }