123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- using CZFW.CMS.Admin;
- using CZFW.Core;
- using CZFW.Framework.Model.ViewModel;
- using CZFW.MDB.Util;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace CZFW.CMS.Builder.Controllers
- {
- public class AttachmentController:AdminControllerBase
- {
- ILogger<AttachmentController> _logger;
- public AttachmentController(ILogger<AttachmentController> logger)
- {
- _logger = logger;
- }
- [HttpPost]
- public ResultModel UploadImages()
- {
- if (!Request.ContentType.Contains("form-data"))
- {
- var result = new ResultModel()
- {
- Success = false,
- Code = "",
- Message = "表单中未找到文件"
- };
- return result;
- } //如果上传文件不是form-data类型抛出异常
- var urls = new List<String>();
- var model = new ResultModel();
- var filePathRoot = ConfigHelper.GetValue<string>("ResourcePathRoot");
- try
- {
- foreach (var f in Request.Form.Files)
- {
- var fileName = $"{CZFW.Core.Utility.GetUnixTimeTick()}" + f.FileName;
- if (!CheckFileExtension(f.FileName, CheckFileTypeEnum.image))
- return Result(false, "文件类型错误,请上传jpg,png,gif类型的图片文件");
- var mimeType = f.Headers["Content-Type"];
- if (!mimeType.ToString().Contains("image"))
- {
- return new ResultModel("文件类型错误!请上传jpg,png,gif类型的图片文件。");
- }
- string date = DateTime.Now.ToString();
- var destPath = Path.Combine(filePathRoot, "TempFiles", fileName);// filePathRoot + "\\TempFiles\\" + fileName;
- var ff = new FileInfo(destPath);
- using (var tt = ff.OpenWrite())
- {
- f.CopyTo(tt);
- tt.Flush();
- tt.Close();
- }
- var destPath2 = Path.Combine(filePathRoot, Site.SiteName, "images", fileName);// $"{filePathRoot}\\{Site.SiteName}\\images\\{fileName}";
- FileInfo flocal = new FileInfo(destPath);
- var tp = flocal.CopyTo(destPath2, true);
- urls.Add($"/{Constants.RESOURCE_FILE_URL_PREFIX}/{Site.SiteName}/images/{fileName}");
- }
- model.Data = urls;
- model.Success = true;
- }
- catch (Exception err)
- {
- model.Success = false;
- model.Message = err.Message;
- }
- return model;
- }
- [HttpPost]
- [RequestSizeLimit(500_000_000)] //最大100m左右
- public ResultModel UploadDocs()
- {
- if (!Request.ContentType.Contains("form-data"))
- {
- var result = new ResultModel()
- {
- Success = false,
- Code = "",
- Message = "表单中未找到文件"
- };
- return result;
- } //如果上传文件不是form-data类型抛出异常
- var urls = new List<String>();
- var model = new ResultModel();
- var filePathRoot = ConfigHelper.GetValue<string>("ResourcePathRoot");
- try
- {
- foreach (var f in Request.Form.Files)
- {
- var fileName = $"{CZFW.Core.Utility.GetUnixTimeTick()}" + f.FileName;
- if (!CheckFileExtension(f.FileName, CheckFileTypeEnum.document))
- return Result(false, "文件类型错误,请上传word,excel,ppt,pdf,zip或者rar类型的文档");
- var mimeType = f.Headers["Content-Type"];
- //if (!mimeType.ToString().Contains("image"))
- //{
- // return new ResultModel("文件类型错误!请上传jpg,png,gif类型的图片文件。");
- //}
- string date = DateTime.Now.ToString();
- var destPath = Path.Combine(filePathRoot, "TempFiles" , fileName);
- var ff = new FileInfo(destPath);
- using (var tt = ff.OpenWrite())
- {
- f.CopyTo(tt);
- tt.Flush();
- tt.Close();
- }
- var destPath2 = Path.Combine(filePathRoot,Site.SiteName,"docs",fileName);
- FileInfo flocal = new FileInfo(destPath);
- var tp = flocal.CopyTo(destPath2, true);
- urls.Add($"/{Constants.RESOURCE_FILE_URL_PREFIX}/{Site.SiteName}/docs/{fileName}");
- }
- model.Data = urls;
- model.Success = true;
- }
- catch (Exception err)
- {
- model.Success = false;
- model.Message = err.Message;
- }
- return model;
- }
- [HttpPost]
- [RequestSizeLimit(500_000_000)] //最大100m左右
- public ResultModel UploadAudio()
- {
- if (!Request.ContentType.Contains("form-data"))
- {
- var result = new ResultModel()
- {
- Success = false,
- Code = "",
- Message = "表单中未找到文件"
- };
- return result;
- } //如果上传文件不是form-data类型抛出异常
- var urls = new List<String>();
- var model = new ResultModel();
- var filePathRoot = ConfigHelper.GetValue<string>("ResourcePathRoot");
- try
- {
- foreach (var f in Request.Form.Files)
- {
- var fileName = $"{CZFW.Core.Utility.GetUnixTimeTick()}" + f.FileName;
- if (!CheckFileExtension(f.FileName, CheckFileTypeEnum.audio))
- return Result(false, "文件类型错误,请上传mp3,wav,mid类型的音频文件");
- var mimeType = f.Headers["Content-Type"];
- if (!mimeType.ToString().Contains("audio"))
- {
- return new ResultModel("文件类型错误!请上传mp3,wav,mid类型的音频文件。");
- }
- string date = DateTime.Now.ToString();
- var destPath = Path.Combine(filePathRoot, "TempFiles", fileName);
- var ff = new FileInfo(destPath);
- using (var tt = ff.OpenWrite())
- {
- f.CopyTo(tt);
- tt.Flush();
- tt.Close();
- }
- var destPath2 = Path.Combine(filePathRoot, Site.SiteName, "audios", fileName);
- FileInfo flocal = new FileInfo(destPath);
- var tp = flocal.CopyTo(destPath2, true);
- urls.Add($"/{Constants.RESOURCE_FILE_URL_PREFIX}/{Site.SiteName}/audios/{fileName}");
- }
- model.Data = urls;
- model.Success = true;
- }
- catch (Exception err)
- {
- model.Success = false;
- model.Message = err.Message;
- }
- return model;
- }
- [HttpPost]
- [RequestSizeLimit(500_000_000)] //最大100m左右
- public ResultModel UploadVideo()
- {
- if (!Request.ContentType.Contains("form-data"))
- {
- var result = new ResultModel()
- {
- Success = false,
- Code = "",
- Message = "表单中未找到文件"
- };
- return result;
- } //如果上传文件不是form-data类型抛出异常
- var urls = new List<String>();
- var model = new ResultModel();
- var filePathRoot = ConfigHelper.GetValue<string>("ResourcePathRoot");
- try
- {
- foreach (var f in Request.Form.Files)
- {
- var fileName = $"{CZFW.Core.Utility.GetUnixTimeTick()}" + f.FileName;
- if (!CheckFileExtension(f.FileName, CheckFileTypeEnum.video))
- return Result(false, "文件类型错误,请上传mp4, avi类型的视频文件");
- var mimeType = f.Headers["Content-Type"];
- if (!mimeType.ToString().Contains("video"))
- {
- return new ResultModel("文件类型错误!请上传mp4, avi类型的视频文件。");
- }
- string date = DateTime.Now.ToString();
- var destPath = Path.Combine(filePathRoot, "TempFiles", fileName);
- var ff = new FileInfo(destPath);
- using (var tt = ff.OpenWrite())
- {
- f.CopyTo(tt);
- tt.Flush();
- tt.Close();
- }
- var destPath2 = Path.Combine(filePathRoot, Site.SiteName, "videos", fileName);
- FileInfo flocal = new FileInfo(destPath);
- var tp = flocal.CopyTo(destPath2, true);
- urls.Add($"/{Constants.RESOURCE_FILE_URL_PREFIX}/{Site.SiteName}/videos/{fileName}");
- }
- model.Data = urls;
- model.Success = true;
- }
- catch (Exception err)
- {
- model.Success = false;
- model.Message = err.Message;
- }
- return model;
- }
- public bool CheckFileExtension(string fileName, CheckFileTypeEnum type)
- {
- var parts = fileName.ToLower().Split('.');
- if(parts.Length<1)
- {
- _logger.LogWarning($"尝试检查文件后缀名异常,文件名:{fileName}");
- return false;
- }
- var extension = parts[parts.Length - 1];
- switch (type)
- {
- case CheckFileTypeEnum.image:
- {
- var imageExtensions =new string[]{"jpg","png","gif","bmp","tif"};
- if (imageExtensions.Contains(extension))
- return true;
- else
- return false;
- }
- case CheckFileTypeEnum.audio:
- {
- var audioExtensions = new string[] { "mp3","aud","wav","mid"};
- if (audioExtensions.Contains(extension))
- return true;
- else
- return false;
- }
- case CheckFileTypeEnum.video:
- {
- var videoExtensions = new string[] { "mp4","avi","mpeg","rmvb","flv"};
- if (videoExtensions.Contains(extension))
- return true;
- else
- return false;
- }
- case CheckFileTypeEnum.document:
- {
- var docExtensions = new string[] { "doc","docx","pdf","ppt","pptx","xls","xlsx","zip","rar"};
- if (docExtensions.Contains(extension))
- return true;
- else
- return false;
- }
- default:
- return false;
- }
- }
- [HttpPost]
- public IActionResult Upload(string theme = "default")
- {
- if (!Request.ContentType.Contains("form-data"))
- {
- var result = new ResultModel()
- {
- Success = false,
- Code = "",
- Message = "表单中未找到文件"
- };
- var res = new { error = 1, message = result.Message };
- return Json(res);
- } //如果上传文件不是form-data类型抛出异常
- var urls = new List<String>();
- var model = new ResultModel();
- var filePathRoot = ConfigHelper.GetValue<string>("ResourcePathRoot");
- try
- {
- foreach (var f in Request.Form.Files)
- {
- var fileName = $"{CZFW.Core.Utility.GetUnixTimeTick()}" + f.FileName;
- if (
- !(CheckFileExtension(f.FileName, CheckFileTypeEnum.image) ||
- CheckFileExtension(f.FileName, CheckFileTypeEnum.audio) ||
- CheckFileExtension(f.FileName, CheckFileTypeEnum.document) ||
- CheckFileExtension(f.FileName, CheckFileTypeEnum.video))
- )
- {
- var res = new { error = 1, message = "文件类型错误" };
- return Json(res);
- }
- string date = DateTime.Now.ToString();
- var destPath = Path.Combine(filePathRoot, "TempFiles", fileName);
- var ff = new FileInfo(destPath);
- using (var tt = ff.OpenWrite())
- {
- f.CopyTo(tt);
- tt.Flush();
- tt.Close();
- }
- var destPathRoot = ConfigHelper.GetValue<string>("SiteViewPathRoot");
- var destPath2 = Path.Combine(filePathRoot, Site.SiteName, "editor_upload", fileName);
- FileInfo flocal = new FileInfo(destPath);
- var tp = flocal.CopyTo(destPath2, true);
- urls.Add($"/{Constants.RESOURCE_FILE_URL_PREFIX}/{Site.SiteName}/editor_upload/{fileName}");
- }
- model.Data = urls;
- model.Success = true;
- }
- catch (Exception err)
- {
- model.Success = false;
- model.Message = err.Message;
- }
- if (model.Success)
- {
- var url = (model.Data as List<string>)[0];
- var res = new { error = 0, url };
- return Json(res);
- }
- else
- {
- var res = new { error = 1, message = model.Message };
- return Json(res);
- }
- }
- }
- public enum CheckFileTypeEnum
- {
- image,
- audio,
- video,
- document
- }
- }
|