602 lines
20 KiB
C#
602 lines
20 KiB
C#
using Hopetry.WebApi.Common;
|
||
using Infrastructure;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.Extensions.Hosting.Internal;
|
||
using Newtonsoft.Json.Linq;
|
||
using PictureFilesApi.Models;
|
||
using PictureFilesApi.Models.RequestModel;
|
||
using PictureFilesApi.Services;
|
||
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
|
||
using System;
|
||
using System.Collections.Concurrent;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Net.Http;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace PictureFilesApi.Controllers
|
||
{
|
||
/// <summary>
|
||
/// 图片服务器控制器
|
||
/// </summary>
|
||
[ApiController]
|
||
[Route("api/[controller]/[action]")]
|
||
public class PlatformController : ControllerBase
|
||
{
|
||
PlatformServices _app;
|
||
private readonly IWebHostEnvironment _hostingEnvironment;
|
||
/// <summary>
|
||
/// 构造函数
|
||
/// </summary>
|
||
/// <param name="app"></param>
|
||
public PlatformController(PlatformServices app, IWebHostEnvironment hostingEnvironment)
|
||
{
|
||
_app = app;
|
||
_hostingEnvironment = hostingEnvironment;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试接口
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[AllowAnonymous]
|
||
[HttpGet]
|
||
public Response<string> Index()
|
||
{
|
||
Response<string> response = new Response<string>();
|
||
try
|
||
{
|
||
response.Result = "222";
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 图片上传接口
|
||
/// </summary>
|
||
/// <param name="files"></param>
|
||
/// <param name="req"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[AllowAnonymous]
|
||
public Response<List<UploadFile>> UploadOld(IFormFileCollection files, [FromQuery] AddOrUpdateUploadReq req)
|
||
{
|
||
req.init();
|
||
Response<List<UploadFile>> response = new Response<List<UploadFile>>();
|
||
try
|
||
{
|
||
response.Result = _app.Upload(files, req);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
/// <summary>
|
||
/// 图片上传接口
|
||
/// </summary>
|
||
/// <param name="files"></param>
|
||
/// <param name="req"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[AllowAnonymous]
|
||
public async Task<Response<List<UploadFile>>> Upload(IFormFileCollection files, [FromQuery] AddOrUpdateUploadReq req)
|
||
{
|
||
req.init();
|
||
Response<List<UploadFile>> response = new Response<List<UploadFile>>();
|
||
try
|
||
{
|
||
response.Result = await _app.UploadWithExif(files, req);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
/// <summary>
|
||
/// 图片删除
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
[AllowAnonymous]
|
||
public Response<bool> DeleteImg(string path)
|
||
{
|
||
|
||
Response<bool> response = new Response<bool>();
|
||
try
|
||
{
|
||
var fullPath = Path.Combine(_hostingEnvironment.ContentRootPath, path);
|
||
response = _app.DeleteImg(fullPath);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
/// <summary>
|
||
/// 判断图片是否存在
|
||
/// </summary>
|
||
/// <param name="path">图片地址</param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
[AllowAnonymous]
|
||
public Response<bool> ExistsImg(string path)
|
||
{
|
||
|
||
Response<bool> response = new Response<bool>();
|
||
try
|
||
{
|
||
var fullPath = Path.Combine(_hostingEnvironment.ContentRootPath, path);
|
||
response = _app.ExistsImg(fullPath);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
/// <summary>
|
||
/// 复制图片
|
||
/// </summary>
|
||
/// <param name="path">图片地址</param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
[AllowAnonymous]
|
||
public Response<string> CopyImg(string path)
|
||
{
|
||
|
||
Response<string> response = new Response<string>();
|
||
try
|
||
{
|
||
var fullPath = Path.Combine(_hostingEnvironment.ContentRootPath, path);
|
||
response = _app.CopyImg(fullPath);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
|
||
|
||
|
||
[HttpPost]
|
||
[AllowAnonymous]
|
||
public Response<List<UploadFile>> UploadNoReName(IFormFileCollection files, [FromQuery] AddOrUpdateUploadReq req)
|
||
{
|
||
req.init();
|
||
Response<List<UploadFile>> response = new Response<List<UploadFile>>();
|
||
try
|
||
{
|
||
response.Result = _app.UploadNoReName(files, req);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 图片上传接口(微信)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[AllowAnonymous]
|
||
public Response<List<UploadFile>> UploadWeChat()
|
||
{
|
||
AddOrUpdateUploadReq req = new AddOrUpdateUploadReq();
|
||
req.init();
|
||
var files = HttpContext.Request.Form.Files;
|
||
Response<List<UploadFile>> response = new Response<List<UploadFile>>();
|
||
try
|
||
{
|
||
response.Result = _app.Upload(files, req);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 图片上传接口(带水印)
|
||
/// </summary>
|
||
/// <param name="files"></param>
|
||
/// <param name="req"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[AllowAnonymous]
|
||
public Response<List<UploadFile>> UploadGraph(IFormFileCollection files,
|
||
[FromQuery] AddOrUpdateUploadGraphReq req)
|
||
{
|
||
req.init();
|
||
Response<List<UploadFile>> response = new Response<List<UploadFile>>();
|
||
try
|
||
{
|
||
response.Result = _app.UploadGraph(files, req);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 东蒙上传图片(带水印)
|
||
/// </summary>
|
||
/// <param name="files"></param>
|
||
/// <param name="uploadReq"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[AllowAnonymous]
|
||
public Response<List<UploadFile>> UploadGraphDongMeng(IFormFileCollection files,
|
||
[FromQuery] AddOrUpdateUploadDongmengReq uploadReq)
|
||
{
|
||
AddOrUpdateUploadGraphReq req = new AddOrUpdateUploadGraphReq();
|
||
req.project = "DongmengViolation";
|
||
req.color = "white";
|
||
req.haveFileName = 0;
|
||
req.haveDateTime = 0;
|
||
req.watermark = "";
|
||
req.watermark += "经纬度:" + uploadReq.lng_lat;
|
||
req.watermark += "\n" + "地址:" + uploadReq.address;
|
||
req.watermark += "\n" + "时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
||
Response<List<UploadFile>> response = new Response<List<UploadFile>>();
|
||
try
|
||
{
|
||
response.Result = _app.UploadGraph(files, req);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下载图片
|
||
/// </summary>
|
||
/// <param name="req"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[AllowAnonymous]
|
||
public Response<string> DownLoadPictures([FromBody] QueryDownLoadPicturesReq req)
|
||
{
|
||
Response<string> response = new Response<string>();
|
||
try
|
||
{
|
||
req.Init();
|
||
response.Result = _app.DownLoadPictures(req);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下载图片
|
||
/// </summary>
|
||
/// <param name="path"></param>
|
||
/// <returns></returns>
|
||
[AllowAnonymous]
|
||
[HttpGet]
|
||
public IActionResult DownLoadPicture(string path)
|
||
{
|
||
try
|
||
{
|
||
//根目录
|
||
var root = AppDomain.CurrentDomain.BaseDirectory;
|
||
|
||
//完整路径
|
||
var fullpath = root + path;
|
||
|
||
//转为流
|
||
var readStream = System.IO.File.ReadAllBytes(fullpath);
|
||
|
||
//文件名
|
||
string fileName = path.Substring(path.LastIndexOf("\\") + 1);
|
||
|
||
//扩展名
|
||
string extName = fileName.Substring(fileName.IndexOf(".") + 1);
|
||
|
||
//返回前台
|
||
return File(readStream, "application/octet-stream", fileName);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下载压缩文件
|
||
/// </summary>
|
||
/// <param name="path"></param>
|
||
/// <returns></returns>
|
||
[AllowAnonymous]
|
||
[HttpGet]
|
||
public IActionResult DownLoadFile(string path)
|
||
{
|
||
try
|
||
{
|
||
//根目录
|
||
var root = AppDomain.CurrentDomain.BaseDirectory;
|
||
|
||
//完整路径
|
||
var fullpath = root + path;
|
||
|
||
//转为流
|
||
var readStream = System.IO.File.ReadAllBytes(fullpath);
|
||
|
||
//文件名
|
||
string fileName = path.Substring(path.LastIndexOf("\\") + 1);
|
||
|
||
//扩展名
|
||
string extName = fileName.Substring(fileName.IndexOf(".") + 1);
|
||
|
||
//fileName = System.Web.HttpUtility.UrlEncode(fileName);
|
||
|
||
//返回前台
|
||
return File(readStream, "application/octet-stream", fileName);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下载网络图片到本地路径
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
public Response<string> DownLoadInternetToLocal([FromBody] AddOrUpdateDownLoadInternetToLocalReq req)
|
||
{
|
||
Response<string> response = new Response<string>();
|
||
try
|
||
{
|
||
response.Result = _app.DownLoadInternetToLocal(req);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
|
||
return response;
|
||
}
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 测试异步上传
|
||
/// </summary>
|
||
/// <param name="Files"></param>
|
||
/// <returns></returns>
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
public async Task<Response<bool>> UploadImages([FromForm] List<StructuredFileDto> Files)
|
||
{
|
||
return await _app.UploadImages(Files);
|
||
}
|
||
|
||
[AllowAnonymous]
|
||
[HttpPost]
|
||
public async Task<Response<bool>> UploadImages1()
|
||
{
|
||
var form = await Request.ReadFormAsync();
|
||
return await _app.UploadImages1(form);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 上传图片并解析附带 Exif(时间、坐标)
|
||
/// </summary>
|
||
/// <param name="files"></param>
|
||
/// <param name="req"></param>
|
||
/// <returns></returns>
|
||
//[HttpPost]
|
||
//[AllowAnonymous]
|
||
//[RequestSizeLimit(1024 * 1024 * 2000)]
|
||
//public Response<List<FileExif>> UploadWithExif(IFormFileCollection files, [FromQuery] AddOrUpdateUploadReq req)
|
||
//{
|
||
// var list = new List<FileExif>();
|
||
|
||
// foreach (var file in files)
|
||
// {
|
||
// var exif = new FileExif();
|
||
|
||
// using (MemoryStream memoryStream = new MemoryStream())
|
||
// {
|
||
// file.CopyTo(memoryStream);
|
||
// memoryStream.Position = 0;
|
||
|
||
// using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(memoryStream))
|
||
// {
|
||
// var exifProfile = image.Metadata.ExifProfile;
|
||
|
||
// if (exifProfile != null)
|
||
// {
|
||
// var latitudeValues = exifProfile.GetValue(ExifTag.GPSLatitude)?.Value;
|
||
// if (latitudeValues != null)
|
||
// {
|
||
// double latitude = Common.ImageHelper.ConvertDMSToDecimal(latitudeValues);
|
||
// exif.Lat = latitude;
|
||
|
||
// exif.IsExif = true;
|
||
// }
|
||
|
||
// var lngitudeValues = exifProfile.GetValue(ExifTag.GPSLongitude)?.Value;
|
||
// if (latitudeValues != null)
|
||
// {
|
||
// double longitude = Common.ImageHelper.ConvertDMSToDecimal(lngitudeValues);
|
||
// exif.Lng = longitude;
|
||
// }
|
||
|
||
// var fileTime = exifProfile.GetValue(ExifTag.DateTimeOriginal)?.Value;
|
||
// if (!string.IsNullOrEmpty(fileTime))
|
||
// {
|
||
// DateTime dateTime = DateTime.ParseExact(fileTime, "yyyy:MM:dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
|
||
// exif.DateTime = dateTime;
|
||
// }
|
||
|
||
// }
|
||
// }
|
||
// }
|
||
// try
|
||
// {
|
||
// var uploadResult = _app.Add(file, req);
|
||
// exif.Path = uploadResult.FilePath;
|
||
// exif.IsUpload = true;
|
||
// list.Add(exif);
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// exif.Remark = $"{file.FileName} 上传失败! {ex.Message}";
|
||
// }
|
||
// }
|
||
|
||
// return new Response<List<FileExif>>
|
||
// {
|
||
// Result = list
|
||
// };
|
||
//}
|
||
|
||
|
||
[HttpPost]
|
||
[AllowAnonymous]
|
||
[RequestSizeLimit(1024 * 1024 * 2000)]
|
||
public Response<ConcurrentBag<FileExif>> UploadWithExif(IFormFileCollection files, [FromQuery] AddOrUpdateUploadReq req)
|
||
{
|
||
var list = new ConcurrentBag<FileExif>();
|
||
|
||
Parallel.ForEach(files, file =>
|
||
{
|
||
var exif = new FileExif();
|
||
|
||
using (MemoryStream memoryStream = new MemoryStream())
|
||
{
|
||
file.CopyTo(memoryStream);
|
||
memoryStream.Position = 0;
|
||
|
||
using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(memoryStream))
|
||
{
|
||
var exifProfile = image.Metadata.ExifProfile;
|
||
|
||
if (exifProfile != null)
|
||
{
|
||
var latitudeValues = exifProfile.GetValue(ExifTag.GPSLatitude)?.Value;
|
||
if (latitudeValues != null)
|
||
{
|
||
double latitude = Common.ImageHelper.ConvertDMSToDecimal(latitudeValues);
|
||
exif.Lat = latitude;
|
||
exif.IsExif = true;
|
||
}
|
||
|
||
var longitudeValues = exifProfile.GetValue(ExifTag.GPSLongitude)?.Value;
|
||
if (longitudeValues != null)
|
||
{
|
||
double longitude = Common.ImageHelper.ConvertDMSToDecimal(longitudeValues);
|
||
exif.Lng = longitude;
|
||
}
|
||
|
||
var fileTime = exifProfile.GetValue(ExifTag.DateTimeOriginal)?.Value;
|
||
if (!string.IsNullOrEmpty(fileTime))
|
||
{
|
||
DateTime dateTime = DateTime.ParseExact(fileTime, "yyyy:MM:dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
|
||
exif.DateTime = dateTime;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
try
|
||
{
|
||
var uploadResult = _app.Add(file, req);
|
||
exif.Path = uploadResult.FilePath;
|
||
exif.IsUpload = true;
|
||
|
||
lock (list)
|
||
{
|
||
list.Add(exif);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
exif.Remark = $"{file.FileName} 上传失败! {ex.Message}";
|
||
}
|
||
});
|
||
|
||
return new Response<ConcurrentBag<FileExif>>
|
||
{
|
||
Result = list
|
||
};
|
||
}
|
||
/// <summary>
|
||
/// 复制图片
|
||
/// </summary>
|
||
/// <param name="path">图片地址</param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[AllowAnonymous]
|
||
public async Task<Response<List<UploadFile>>> NewCopyImg([FromForm] string path)
|
||
{
|
||
|
||
var response = new Response<List<UploadFile>>();
|
||
try
|
||
{
|
||
//var fullPath = Path.Combine(_hostingEnvironment.ContentRootPath, path);
|
||
response.Result = await _app.NewCopyImg(path);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
response.Code = 500;
|
||
response.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
|
||
}
|
||
return response;
|
||
}
|
||
}
|
||
} |