summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/DeveloperController.cs
blob: aed4eb57b176fe9f14fe7ea783a7cf1f2c78f284 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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()
        {
            if (!ACL.Granted(User.Identity.Name, "CanAccessDevCP"))
                return new HttpStatusCodeResult(403);
            ViewBag.Developer = true;
            return View();
        }

        public ActionResult Releases()
        {
            if (!ACL.Granted(User.Identity.Name, "CanAccessDevCP"))
                return new HttpStatusCodeResult(403);
            var db = new ApplicationDbContext();
            return View(db.Downloads);
        }

        public ActionResult AddRelease()
        {
            if (!ACL.Granted(User.Identity.Name, "CanAccessDevCP"))
                return new HttpStatusCodeResult(403);
            if (!ACL.Granted(User.Identity.Name, "CanReleaseBuild"))
                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, "CanAccessDevCP"))
                return new HttpStatusCodeResult(403);
            if (!ACL.Granted(User.Identity.Name, "CanReleaseBuild"))
                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) || 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.ToLower();
            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.Remove(0, 1);
            //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.ToLower(); ;
            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.Remove(0,1);
            model.Screenshot.SaveAs(mapped_dir);

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

            return RedirectToAction("Releases");
        }
        
        [Authorize]
        public ActionResult Wiki()
        {
            if (!ACL.Granted(User.Identity.Name, "CanAccessDevCP"))
                return new HttpStatusCodeResult(403);
            ViewBag.Developer = true;
            var db = new ApplicationDbContext();
            var cats = db.WikiCategories;
            return View(cats);
        }

        public ActionResult AddWikiCategory()
        {
            if (!ACL.Granted(User.Identity.Name, "CanAccessDevCP"))
                return new HttpStatusCodeResult(403);

            ViewBag.Developer = true;

            var mdl = new AddWikiCategoryViewModel();
            return View(mdl);

        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddWikiCategory(AddWikiCategoryViewModel model)
        {
            if (!ACL.Granted(User.Identity.Name, "CanAccessDevCP"))
                return new HttpStatusCodeResult(403);
            ViewBag.Developer = true;
            if (!ModelState.IsValid)
                return View(model);

            var db = new ApplicationDbContext();
            var cat = new WikiCategory();

            var new_id = model.Name.ToLower();

            foreach(var c in new_id.ToArray())
            {
                if(c == '.' || !ApprovedIdChars.Contains(c))
                {
                    new_id = new_id.Replace(c, '_');
                }
            }

            cat.Id = new_id;
            cat.Name = model.Name;

            cat.Parent = model.ParentId;

            db.WikiCategories.Add(cat);
            db.SaveChanges();

            return RedirectToAction("Wiki");



        }

    }


}