Light12306/ChatRoomServer.Www/Areas/Api/Controllers/PicController.cs
2015-07-08 17:24:45 +08:00

62 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace ChatRoomServer.Www.Areas.Api.Controllers
{
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Hosting;
public class PicController : ApiController
{
//[AllowAnonymous]
//[HttpPost]
//[Route("upload")]
//public async Task<string[]> Upload()
//{
// if (!Request.Content.IsMimeMultipartContent())
// throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
// var provider = new MultipartMemoryStreamProvider();
// await Request.Content.ReadAsMultipartAsync(provider);
// var root = HostingEnvironment.MapPath("~/content/imgs/");
// var dirTpl = $"$1{Path.PathSeparator}$2";
// Directory.CreateDirectory(root);
// var result = new List<string>();
// foreach (var content in provider.Contents)
// {
// var filename = content.Headers.ContentDisposition.FileName;
// if (filename.IsNullOrEmpty() || !Regex.IsMatch(filename, @"\.(jpg|png|gif|jpeg)$", RegexOptions.IgnoreCase))
// {
// continue;
// }
// var data = await content.ReadAsByteArrayAsync();
// //compute md5
// var md5 = data.Md5String();
// var subPath = Regex.Replace(md5, @"^([a-f\d]{2})([a-f\d]{2})([a-f\d]+)", dirTpl);
// var dir = Path.Combine(root, subPath);
// var fullpath = Path.Combine(dir, md5 + Path.GetExtension(filename));
// if (!File.Exists(fullpath))
// {
// Directory.CreateDirectory(dir);
// File.WriteAllBytes(fullpath, data);
// }
// result.Add("");
// }
//}
}
}