55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using Bring2mind.InMemoriam.Core.Models.Pictures;
|
|
using System;
|
|
using System.IO;
|
|
using System.Web;
|
|
|
|
namespace Bring2mind.InMemoriam.Core
|
|
{
|
|
public class PicturesService
|
|
{
|
|
public static PictureBase UploadPicture(string portalMapPath, int moduleId, HttpPostedFile file)
|
|
{
|
|
var newGuid = Guid.NewGuid();
|
|
var filePath = Path.Combine(portalMapPath, Common.Globals.uploadPath, moduleId.ToString());
|
|
if (!Directory.Exists(filePath))
|
|
{
|
|
Directory.CreateDirectory(filePath);
|
|
}
|
|
var fileName = Path.Combine(filePath, newGuid.ToString() + ".resources");
|
|
file.SaveAs(fileName);
|
|
|
|
var res = new PictureBase
|
|
{
|
|
ImageIdentifier = newGuid,
|
|
OriginalName = file.FileName,
|
|
OriginalWidth = 0,
|
|
OriginalHeight = 0
|
|
};
|
|
|
|
// detect image size
|
|
using (var img = System.Drawing.Image.FromFile(fileName))
|
|
{
|
|
res.OriginalHeight = img.Height;
|
|
res.OriginalWidth = img.Width;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
public static void DeletePicture(string portalMapPath, int moduleId, Guid fileId)
|
|
{
|
|
var imagesDir = new DirectoryInfo(Path.Combine(portalMapPath, Common.Globals.uploadPath, moduleId.ToString()));
|
|
var files = imagesDir.GetFiles($"{fileId}*.jpg");
|
|
foreach (var file in files)
|
|
{
|
|
file.Delete();
|
|
}
|
|
var originalFile = Path.Combine(portalMapPath, Common.Globals.uploadPath, fileId.ToString() + ".resources");
|
|
if (File.Exists(originalFile))
|
|
{
|
|
File.Delete(originalFile);
|
|
}
|
|
}
|
|
}
|
|
}
|