LaJiFenLei/Waste.Web.Entry/Pages/Index.cshtml.cs

230 lines
9.5 KiB
C#

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using Nirvana.Common;
using Nirvana.Common.File;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Waste.Domain;
namespace Waste.Web.Entry.Pages
{
public class IndexModel : BaseModel
{
private static readonly FormOptions _defaultFormOptions = new FormOptions();
private readonly IWebHostEnvironment _hostingEnvironment;
public IndexModel(IWebHostEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
#region
/// <summary>
/// 获取枚举类
/// </summary>
/// <returns></returns>
public JsonResult OnGetClientsData()
{
var data = new
{
StatusType = this.StatusType(),
AccountType = this.AccountType(),
DeviceStatus = this.DeviceStatus(),
DeviceType = this.DeviceType(),
DeviceNetType = this.DeviceNetType(),
AdStatus =this.AdStatus(),
DeviceNetStatus = this.DeviceNetStatus()
};
return Success(data);
}
private object DeviceNetStatus()
{
return EnumHelper.GetEnumDictionary<DeviceNetStatus>();
}
private object StatusType()
{
return EnumHelper.GetEnumDictionary<StatusType>();
}
private object AccountType()
{
return EnumHelper.GetEnumDictionary<AccountType>();
}
private object DeviceStatus()
{
return EnumHelper.GetEnumDictionary<DeviceStatus>();
}
private object DeviceType()
{
return EnumHelper.GetEnumDictionary<DeviceType>();
}
private object DeviceNetType()
{
return EnumHelper.GetEnumDictionary<DeviceNetType>();
}
private object AdStatus()
{
return EnumHelper.GetEnumDictionary<AdStatus>();
}
/// <summary>
/// 文件上传
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
public async Task<JsonResult> OnPostUploadAsync(string root = "banner")
{
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
{
return Fail("只支持multipart/form-data类型");
}
var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, HttpContext.Request.Body);
var section = await reader.ReadNextSectionAsync();
List<string> filelist = new List<string>();
while (section != null)
{
var hasContentDispositionHeader =
ContentDispositionHeaderValue.TryParse(
section.ContentDisposition, out var contentDisposition);
if (hasContentDispositionHeader)
{
if (!MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
{
}
else
{
string[] _permittedExtensions = { ".gif", ".jpg", ".jpeg", ".png", ".bmp" };
long _fileSizeLimit = 1048576 * 5;
string rootname = $"/images/{root}/{DateTime.Now.ToString("yyyyMMdd")}";
string savefolder = _hostingEnvironment.WebRootPath;
var name = Path.GetRandomFileName() + ".jpg";
var streamedFileContent = await FileHelpers.ProcessStreamedFile(
section, contentDisposition, ModelState,
_permittedExtensions, _fileSizeLimit);
if (!ModelState.IsValid)
{
string msg = "上传失败";
if (ModelState.Values.Count() > 0)
{
msg = ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage;
}
return Fail(msg);
}
var thumbnailPath = Path.Combine(savefolder + "/" + rootname, name);
if (!Directory.Exists(Path.GetDirectoryName(thumbnailPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(thumbnailPath));
}
else
{
//检查文件是否存在
if (System.IO.File.Exists(thumbnailPath))
{
return Fail("此文件已经存在");
}
}
var filepath = Path.Combine("wwwroot" + rootname + "/", name);
var virualpath = Path.Combine(rootname + "/", name);
var filename = contentDisposition.FileName.Value;
using (var targetStream = System.IO.File.Create(filepath))
{
await targetStream.WriteAsync(streamedFileContent);
filelist.Add(virualpath);
}
}
}
section = await reader.ReadNextSectionAsync();
}
return Success(filelist);
}
/// <summary>
/// 视频文件上传
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
public async Task<JsonResult> OnPostUploadVideoAsync(string root = "banner")
{
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
{
return Fail("只支持multipart/form-data类型");
}
var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, HttpContext.Request.Body);
var section = await reader.ReadNextSectionAsync();
List<string> filelist = new List<string>();
while (section != null)
{
var hasContentDispositionHeader =
ContentDispositionHeaderValue.TryParse(
section.ContentDisposition, out var contentDisposition);
if (hasContentDispositionHeader)
{
if (!MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
{
}
else
{
string[] _permittedExtensions = { ".mp4" };
long _fileSizeLimit = 1048576 * 500;
string rootname = $"/images/{root}/{DateTime.Now.ToString("yyyyMMdd")}";
string savefolder = _hostingEnvironment.WebRootPath;
var name = Path.GetRandomFileName() + ".mp4";
var streamedFileContent = await FileHelpers.ProcessStreamedFile(
section, contentDisposition, ModelState,
_permittedExtensions, _fileSizeLimit);
if (!ModelState.IsValid)
{
string msg = "上传失败";
if (ModelState.Values.Count() > 0)
{
msg = ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage;
}
return Fail(msg);
}
var thumbnailPath = Path.Combine(savefolder + "/" + rootname, name);
if (!Directory.Exists(Path.GetDirectoryName(thumbnailPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(thumbnailPath));
}
else
{
//检查文件是否存在
if (System.IO.File.Exists(thumbnailPath))
{
return Fail("此文件已经存在");
}
}
var filepath = Path.Combine("wwwroot" + rootname + "/", name);
var virualpath = Path.Combine(rootname + "/", name);
var filename = contentDisposition.FileName.Value;
using (var targetStream = System.IO.File.Create(filepath))
{
await targetStream.WriteAsync(streamedFileContent);
filelist.Add(virualpath);
}
}
}
section = await reader.ReadNextSectionAsync();
}
return Success(filelist);
}
#endregion
public void OnGet()
{
}
}
}