summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/SkinsController.cs
blob: 3f8da3c586b55dada3ff19730ed145700898ccf8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Project_Unite.Models;

namespace Project_Unite.Controllers
{
    public class SkinsController : Controller
    {
        // GET: Skins
        public ActionResult Index()
        {
            return View(new ApplicationDbContext().Skins);
        }

        public ActionResult ViewSkin(string id)
        {
            var db = new ApplicationDbContext();
            var skn = db.Skins.FirstOrDefault(x => x.Id == id);
            if (skn == null)
                return new HttpStatusCodeResult(404);
            return View(skn);
        }

        [Authorize]
        public ActionResult PostSkin()
        {
            var model = new CreateSkinViewModel();
            return View(model);
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult PostSkin(CreateSkinViewModel model)
        {
            if(!ModelState.IsValid)
            {
                return View(model);
            }
            var db = new ApplicationDbContext();
            var skin = new Skin();

            string allowed = "abcdefghijklmnopqrstuvwxyz1234567890-_";

            string id = model.Title.ToLower();
            foreach(char c in id.ToCharArray())
            {
                if (!allowed.Contains(c))
                    id = id.Replace(c, '_');
            }

            skin.Id = id + "_" + db.Skins.Count().ToString();
            skin.Name = model.Title;
            skin.ShortDescription = model.ShortDescription;
            skin.PostedAt = DateTime.Now;
            skin.FullDescription = model.LongDescription;
            skin.UserId = User.Identity.GetUserId();
            skin.VersionId = "";
            string repoFolder = $"~/Uploads/{ACL.UserNameRaw(skin.UserId)}/SkinFiles";
            string screenshotFolder = $"~/Uploads/{ACL.UserNameRaw(skin.UserId)}/Screenshots";
            if (!Directory.Exists(Server.MapPath(repoFolder)))
                Directory.CreateDirectory(Server.MapPath(repoFolder));
            if (!Directory.Exists(Server.MapPath(screenshotFolder)))
                Directory.CreateDirectory(Server.MapPath(screenshotFolder));


            int len = model.SkinFile.FileName.Length;

            int index = model.SkinFile.FileName.LastIndexOf(".");
            int end = len - index;
            skin.DownloadUrl = repoFolder.Remove(0,1) + "/" + model.SkinFile.FileName.Remove(index,end) + ".zip";



            string work_dir = Server.MapPath("~/unite_work_" + Guid.NewGuid().ToString());

            if (model.SkinFile.FileName.ToLower().EndsWith(".zip"))
            {
                work_dir = Server.MapPath(repoFolder);
            }
            else
            {

                if (Directory.Exists(work_dir))
                    Directory.Delete(work_dir, true);

                Directory.CreateDirectory(work_dir);
            }
            model.SkinFile.SaveAs(Path.Combine(work_dir, model.SkinFile.FileName));
            if (!model.SkinFile.FileName.ToLower().EndsWith(".zip"))
            {
                ZipFile.CreateFromDirectory(work_dir, Server.MapPath("~" + skin.DownloadUrl));
                Directory.Delete(work_dir, true);
            }
            
                if (model.ScreenshotFile != null && model.ScreenshotFile.ContentLength > 0)
            {
                skin.ScreenshotUrl = screenshotFolder.Remove(0, 1) + "/" + model.ScreenshotFile.FileName;
                model.ScreenshotFile.SaveAs(Path.Combine(Server.MapPath(screenshotFolder), model.ScreenshotFile.FileName));
            }
            db.Skins.Add(skin);
            db.SaveChanges();
            return RedirectToAction("ViewSkin", new { id = skin.Id });
        }
    }
}