summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/DeveloperController.cs
blob: 0825eb1a6ae914871d9682a3fda9ca8b47a7404b (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Project_Unite.Models;

namespace Project_Unite.Controllers
{
    [Authorize]
    public class DeveloperController : Controller
    {
        // GET: Developer
        public ActionResult Index()
        {
            ViewBag.Developer = true;
            return View();
        }

        public ActionResult Releases()
        {
            var db = new ApplicationDbContext();
            return View(db.Downloads);
        }

        public ActionResult AddRelease()
        {
            if (!ACL.Granted(User.Identity.Name, "CanReleaseBuilds"))
                return new HttpStatusCodeResult(403);
            ViewBag.Developer = true;

            var build = new PostDownloadViewModel();
            return View(build);
        }

        const string ApprovedIdChars = ".-_abcdefghijklmnopqrstuvwxyz1234567890";

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddRelease(PostDownloadViewModel model)
        {
            if (!ACL.Granted(User.Identity.Name, "CanReleaseBuilds"))
                return new HttpStatusCodeResult(403);
            if (!ModelState.IsValid)
                return View(model);

            //Create a new database context.
            var db = new ApplicationDbContext();

            //Create a new "Download" data object
            var download = new Download();
            //Set the ID.
            string new_id = model.Name.ToLower();

            foreach(var c in new_id.ToCharArray())
            {
                if (!ApprovedIdChars.Contains(c))
                    new_id = new_id.Replace(c, '_');
            }
            new_id += "_" + db.Downloads.Count().ToString();
            download.Id = new_id;
            //Set the name.
            download.Name = model.Name;
            //Set the author
            download.ReleasedBy = User.Identity.GetUserId();
            //Set the time
            download.PostDate = DateTime.Now;

            //Can't forget the changelog. Literally, I almost forgot.
            download.Changelog = model.Changelog;

            //Set the YT ID
            download.DevUpdateId = model.DevUpdateId;

            //Set whether the build is stable
            download.IsStable = model.IsStable;

            //We're not obsolete.
            download.Obsolete = false;

            //Now we upload the download.

            string download_dir = "~/Uploads/Releases/";
            string mapped_dir = Server.MapPath(download_dir);
            if (!Directory.Exists(mapped_dir))
                Directory.CreateDirectory(mapped_dir);

            string file_name_d = model.Download.FileName;
            foreach(var c in file_name_d.ToCharArray())
            {
                if (!ApprovedIdChars.Contains(c))
                    file_name_d = file_name_d.Replace(c, '_');
            }
            download_dir += file_name_d;
            mapped_dir = Server.MapPath(download_dir);
            download.DownloadUrl = download_dir;
            //Now the download is saved in the DB. Let's get it on the server.
            model.Download.SaveAs(mapped_dir);

            download_dir = "~/Uploads/Releases/Screenshots/";
            mapped_dir = Server.MapPath(download_dir);
            if (!Directory.Exists(mapped_dir))
                Directory.CreateDirectory(mapped_dir);

            file_name_d = model.Screenshot.FileName;
            foreach (var c in file_name_d.ToCharArray())
            {
                if (!ApprovedIdChars.Contains(c))
                    file_name_d = file_name_d.Replace(c, '_');
            }
            download_dir += file_name_d;
            mapped_dir = Server.MapPath(download_dir);
            download.ScreenshotUrl = download_dir;
            model.Screenshot.SaveAs(mapped_dir);

            //Now we just save to the database...
            db.Downloads.Add(download);
            db.SaveChanges();

            return RedirectToAction("Releases");
        }
    }


}