MMD资源保存器。

This commit is contained in:
liteng 2018-09-26 07:46:03 +08:00
parent 08802e3259
commit dec0258306
6 changed files with 239 additions and 0 deletions

View File

@ -50,5 +50,10 @@ namespace ShadowEditor.Server
/// 音频表名
/// </summary>
public const string AudioCollectionName = "_Audio";
/// <summary>
/// MMD表名
/// </summary>
public const string MMDCollectionName = "_MMD";
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace ShadowEditor.Server.MMD
{
/// <summary>
/// MMD保存器接口
/// </summary>
public interface IMMDSaver
{
/// <summary>
/// 保存模型
/// </summary>
/// <param name="context">Web上下文环境</param>
/// <returns></returns>
Result Save(HttpContext context);
}
}

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShadowEditor.Server.MMD
{
/// <summary>
/// MMD信息
/// </summary>
public class MMDInfo
{
/// <summary>
/// 资源名称(改名前等同于上传文件名称)
/// </summary>
public string Name { get; set; }
/// <summary>
/// 全拼
/// </summary>
public string TotalPinYin { get; set; }
/// <summary>
/// 首字母拼音
/// </summary>
public string FirstPinYin { get; set; }
/// <summary>
/// 资源类型
/// </summary>
public MMDType Type { get; set; }
/// <summary>
/// 下载地址
/// </summary>
public string Url { get; set; }
/// <summary>
/// 上传文件名称
/// </summary>
public string FileName { get; set; }
/// <summary>
/// 文件大小
/// </summary>
public int FileSize { get; set; }
/// <summary>
/// 文件类型
/// </summary>
public string FileType { get; set; }
/// <summary>
/// 保存文件名称
/// </summary>
public string SaveName { get; set; }
/// <summary>
/// 保存路径
/// </summary>
public string SavePath { get; set; }
/// <summary>
/// 上传时间
/// </summary>
public DateTime AddTime { get; set; }
/// <summary>
/// 缩略图
/// </summary>
public string Thumbnail { get; set; }
}
}

View File

@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using MongoDB.Bson;
using ShadowEditor.Server.Helpers;
namespace ShadowEditor.Server.MMD
{
/// <summary>
/// MMD保存器
/// </summary>
public class MMDSaver : IMMDSaver
{
public Result Save(HttpContext context)
{
var Request = context.Request;
var Server = context.Server;
// 文件信息
var file = Request.Files[0];
var fileName = file.FileName;
var fileSize = file.ContentLength;
var fileType = file.ContentType;
var fileExt = Path.GetExtension(fileName);
var fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);
if (fileExt == null || fileExt.ToLower() != ".zip")
{
return new Result
{
Code = 300,
Msg = "只允许上传zip格式文件"
};
}
// 保存文件
var now = DateTime.Now;
var savePath = $"/Upload/MMD/{now.ToString("yyyyMMddHHmmss")}";
var physicalPath = Server.MapPath(savePath);
var tempPath = physicalPath + "\\temp"; // zip压缩文件临时保存目录
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
file.SaveAs($"{tempPath}\\{fileName}");
// 解压文件
ZipHelper.Unzip($"{tempPath}\\{fileName}", physicalPath);
// 删除临时目录
Directory.Delete(tempPath, true);
// 判断文件类型
string entryFileName = null;
var meshType = MMDType.unknown;
var files = Directory.GetFiles(physicalPath);
if (files.Where(o => o.ToLower().EndsWith(".pmd")).Count() > 0) // 模型文件
{
entryFileName = files.Where(o => o.ToLower().EndsWith(".pmd")).FirstOrDefault();
entryFileName = $"{savePath}/{Path.GetFileName(entryFileName)}";
meshType = MMDType.pmd;
}
else if (files.Where(o => o.ToLower().EndsWith(".pmx")).Count() > 0) // 模型文件
{
entryFileName = files.Where(o => o.ToLower().EndsWith(".pmx")).FirstOrDefault();
entryFileName = $"{savePath}/{Path.GetFileName(entryFileName)}";
meshType = MMDType.pmx;
}
else if (files.Where(o => o.ToLower().EndsWith(".vmd")).Count() > 0) // 动画文件
{
entryFileName = files.Where(o => o.ToLower().EndsWith(".vmd")).FirstOrDefault();
entryFileName = $"{savePath}/{Path.GetFileName(entryFileName)}";
meshType = MMDType.vmd;
}
var pinyin = PinYinHelper.GetTotalPinYin(fileNameWithoutExt);
// 保存到Mongo
var mongo = new MongoHelper();
var doc = new BsonDocument();
doc["AddTime"] = BsonDateTime.Create(now);
doc["FileName"] = fileName;
doc["FileSize"] = fileSize;
doc["FileType"] = fileType;
doc["FirstPinYin"] = string.Join("", pinyin.FirstPinYin);
doc["Name"] = fileNameWithoutExt;
doc["SaveName"] = fileName;
doc["SavePath"] = savePath;
doc["Thumbnail"] = "";
doc["TotalPinYin"] = string.Join("", pinyin.TotalPinYin);
doc["Type"] = meshType.ToString();
doc["Url"] = entryFileName;
mongo.InsertOne(Constant.MMDCollectionName, doc);
return new Result
{
Code = 200,
Msg = "上传成功!"
};
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShadowEditor.Server.MMD
{
/// <summary>
/// 资源类型
/// </summary>
public enum MMDType
{
unknown, // 未知类型
pmd, // 模型文件
pmx, // 模型文件
vmd, // 动画文件
}
}

View File

@ -105,6 +105,7 @@
<Compile Include="Constant.cs" />
<Compile Include="Controllers\MeshController.cs" />
<Compile Include="Controllers\AudioController.cs" />
<Compile Include="Controllers\MMDController.cs" />
<Compile Include="Controllers\TextureController.cs" />
<Compile Include="Controllers\SceneController.cs" />
<Compile Include="Helpers\DownloadHelper.cs" />
@ -119,6 +120,10 @@
<Compile Include="Mesh\MeshInfo.cs" />
<Compile Include="Mesh\MeshSaver.cs" />
<Compile Include="Mesh\MeshType.cs" />
<Compile Include="MMD\IMMDSaver.cs" />
<Compile Include="MMD\MMDInfo.cs" />
<Compile Include="MMD\MMDSaver.cs" />
<Compile Include="MMD\MMDType.cs" />
<Compile Include="Result.cs" />
<Compile Include="Scene\SaveSceneModel.cs" />
<Compile Include="Scene\SceneModel.cs" />