123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using System.DrawingCore.Imaging;
- using System.IO;
- using Library;
- using MySystem.Areas.Admin.Controllers;
- using System.Collections;
- using LitJson;
- using System.Globalization;
- using System.Web;
- // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
- namespace MySystem.Areas.Api.Controllers
- {
- [Area("Api")]
- [Route("Api/[controller]/[action]")]
- public class PublicMethodController : Admin.Controllers.BaseController
- {
- public PublicMethodController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
- {
- }
- #region 图片验证码
- public FileContentResult CheckCode()
- {
- string code = function.get_Random(4);
- var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
- MemoryStream stream = new MemoryStream();
- bitmap.Save(stream, ImageFormat.Gif);
- function.WriteCookie(_accessor.HttpContext, "checkcode", code);
- return File(stream.ToArray(), "image/gif");
- }
- #endregion
-
- #region 图片验证码
- public FileContentResult PictureCode(string Tag = "")
- {
- string code = function.get_Random(4);
- var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
- MemoryStream stream = new MemoryStream();
- bitmap.Save(stream, ImageFormat.Gif);
- RedisDbconn.Instance.Set(Tag, code);
- RedisDbconn.Instance.SetExpire(Tag, 600);
- return File(stream.ToArray(), "image/jpg");
- }
- #endregion
- #region 编辑器上传图片
- public object EditorUpload([FromForm] IFormCollection rf)
- {
- //文件保存目录路径
- String savePath = "/up/";
- //文件保存目录URL
- String saveUrl = "/up/";
- //定义允许上传的文件扩展名
- Hashtable extTable = new Hashtable();
- extTable.Add("image", "gif,jpg,jpeg,png,bmp");
- extTable.Add("flash", "swf,flv");
- extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4");
- extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
- //最大文件大小
- long maxSize = 1073741824000;
- IFormFile imgFile = rf.Files["imgFile"];
- if (imgFile == null)
- {
- showError("请选择文件。");
- }
- String dirPath = function.getPath(MySystem.OssHelper.Instance.PathName + savePath);
- if (!Directory.Exists(dirPath))
- {
- //showError("上传目录不存在。");
- Directory.CreateDirectory(dirPath);
- }
- String fileName = imgFile.FileName;
- String dirName = "";
- if (String.IsNullOrEmpty(dirName))
- {
- if (fileName.ToLower().EndsWith(".mp4") || fileName.ToLower().EndsWith(".mp3"))
- {
- dirName = "media";
- }
- else
- {
- dirName = "image";
- }
- }
- if (!extTable.ContainsKey(dirName))
- {
- showError("目录名不正确。");
- }
- String fileExt = Path.GetExtension(fileName).ToLower();
- if (imgFile.Length > maxSize)
- {
- showError("上传文件大小超过限制。");
- }
- if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
- {
- showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
- }
- //创建文件夹
- dirPath += dirName + "/";
- saveUrl += dirName + "/";
- if (!Directory.Exists(dirPath))
- {
- Directory.CreateDirectory(dirPath);
- }
- String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
- dirPath += ymd + "/";
- saveUrl += ymd + "/";
- if (!Directory.Exists(dirPath))
- {
- Directory.CreateDirectory(dirPath);
- }
- String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
- String filePath = dirPath + newFileName;
- using var fs = new FileStream(filePath, FileMode.Create);
- imgFile.CopyTo(fs);
- String fileUrl = saveUrl + newFileName;
- if (dirName != "media")
- {
- if (!function.check_pic(filePath))
- {
- System.IO.File.Delete(filePath);
- return null;
- }
- if (function.getImgRule(filePath)["width"] > 2000)
- {
- String newFileName_sl = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + "_sl" + fileExt;
- function.imgcut(2000, filePath, dirPath + newFileName_sl);
- System.IO.File.Delete(filePath);
- fileUrl = saveUrl + newFileName_sl;
- }
- }
- Hashtable hash = new Hashtable();
- hash["error"] = 0;
- hash["url"] = MySystem.OssHelper.Instance.SourceHost + "/" + MySystem.OssHelper.Instance.PathName + fileUrl;
- OssHelper.Instance.ScanQueue(fileUrl, "");
- return JsonMapper.ToJson(hash);
- }
- private object showError(string message)
- {
- Hashtable hash = new Hashtable();
- hash["error"] = 1;
- hash["message"] = message;
- return JsonMapper.ToJson(hash);
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- #endregion
- #region 系统-上传文件
- public JsonResult UploadFile([FromForm] IFormCollection rf)
- {
- IFormFile imgFile = rf.Files["file"];
- string path = MySystemLib.SystemPublicFuction.GetFilePath(imgFile, OssHelper.Instance.PathName + "/uploadfile/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
- OssHelper.Instance.ScanQueue(path, "");
- Dictionary<string, string> obj = new Dictionary<string, string>();
- obj.Add("FileName", path);
- return Json(obj);
- }
- #endregion
- #region 系统-上传图片
- public JsonResult UploadPhoto([FromForm] IFormCollection rf)
- {
- IFormFile imgFile = rf.Files["Icon"];
- string Icon = MySystemLib.SystemPublicFuction.GetPicPath(imgFile, OssHelper.Instance.PathName + "/upload/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
- OssHelper.Instance.ScanQueue(Icon, "");
- return Json(new AppResultJson() { Status = "1", Info = "", Data = Icon });
- }
- #endregion
- #region 系统-上传图片
- public JsonResult UploadPhotoByBase64(string value)
- {
- value = value.Replace("data:image/png;base64,", "");
- string base64str = HttpUtility.UrlDecode(value).Replace(" ", "+");
- string dummyData = base64str.Replace("%", "").Replace(",", "").Replace(" ", "+");
- if (dummyData.Length % 4 > 0)
- {
- dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
- }
- string Icon = function.base64StringToImage(dummyData, "/" + OssHelper.Instance.PathName + "/upload/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/", "MT" + function.MD5_16(Guid.NewGuid().ToString()) + ".png");
- // OssHelper.Instance.ScanQueue(Icon, "");
- return Json(new AppResultJson() { Status = "1", Info = "", Data = Icon });
- }
- #endregion
- #region 系统-layui上传文件
- public JsonResult LayUIUpload([FromForm] IFormCollection rf, string Path = "", string Resize = "")
- {
- IFormFile imgFile = rf.Files[0];
- string Icon = MySystemLib.SystemPublicFuction.GetFilePath(imgFile, Path + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/");
- string piccut = "";
- if (!string.IsNullOrEmpty(Resize) && (imgFile.FileName.ToLower().EndsWith(".png") || imgFile.FileName.EndsWith(".jpg")))
- {
- JsonData data = JsonMapper.ToObject(Resize);
- int width = int.Parse(data["width"].ToString());
- int height = int.Parse(data["height"].ToString());
- int quality = int.Parse(data["quality"].ToString());
- piccut = Icon.Replace(".", "s.");
- function.imgcut2(width, height, function.getPath(Icon), function.getPath(piccut));
- System.IO.File.Delete(function.getPath(Icon));
- }
- else
- {
- piccut = Icon;
- }
- return Json(new AppResultJson() { Status = "1", Info = "", Data = piccut });
- }
- #endregion
- #region 图片转base64
- public string ImgToBase64(string path)
- {
- path = HttpUtility.UrlDecode(path);
- return function.imageToBase64String(function.getPath(path));
- }
- #endregion
- }
- }
|