forked from Teknikode/Teknik
7 changed files with 257 additions and 30 deletions
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
using System.Web.Mvc; |
||||
|
||||
namespace Teknik.Areas.API |
||||
{ |
||||
public class APIAreaRegistration : AreaRegistration |
||||
{ |
||||
public override string AreaName |
||||
{ |
||||
get |
||||
{ |
||||
return "API"; |
||||
} |
||||
} |
||||
|
||||
public override void RegisterArea(AreaRegistrationContext context) |
||||
{ |
||||
#region API v1
|
||||
// Base Routing
|
||||
context.MapSubdomainRoute( |
||||
"API.Index.v1", // Route name
|
||||
"dev", |
||||
"API/v1", // URL with parameters
|
||||
new { controller = "API", action = "Index_v1" }, // Parameter defaults
|
||||
new[] { typeof(Controllers.APIController).Namespace } |
||||
); |
||||
context.MapSubdomainRoute( |
||||
"API.Index.v1", // Route name
|
||||
"api", |
||||
"v1", // URL with parameters
|
||||
new { controller = "API", action = "Index_v1" }, // Parameter defaults
|
||||
new[] { typeof(Controllers.APIController).Namespace } |
||||
); |
||||
// Uploads
|
||||
context.MapSubdomainRoute( |
||||
"API.Upload.v1", // Route name
|
||||
"dev", |
||||
"API/v1/Upload", // URL with parameters
|
||||
new { controller = "API", action = "Upload_v1" }, // Parameter defaults
|
||||
new[] { typeof(Controllers.APIController).Namespace } |
||||
); |
||||
context.MapSubdomainRoute( |
||||
"API.Upload.v1", // Route name
|
||||
"api", |
||||
"v1/Upload", // URL with parameters
|
||||
new { controller = "API", action = "Upload_v1" }, // Parameter defaults
|
||||
new[] { typeof(Controllers.APIController).Namespace } |
||||
); |
||||
#endregion
|
||||
|
||||
// Default Routing
|
||||
context.MapSubdomainRoute( |
||||
"API.Index", // Route name
|
||||
"dev", |
||||
"API", // URL with parameters
|
||||
new { controller = "API", action = "Index" }, // Parameter defaults
|
||||
new[] { typeof(Controllers.APIController).Namespace } |
||||
); |
||||
context.MapSubdomainRoute( |
||||
"API.Index", // Route name
|
||||
"api", |
||||
"", // URL with parameters
|
||||
new { controller = "API", action = "Index" }, // Parameter defaults
|
||||
new[] { typeof(Controllers.APIController).Namespace } |
||||
); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,131 @@
@@ -0,0 +1,131 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Data.Entity; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Web; |
||||
using System.Web.Mvc; |
||||
using Teknik.Areas.Upload; |
||||
using Teknik.Controllers; |
||||
using Teknik.Helpers; |
||||
using Teknik.Models; |
||||
|
||||
namespace Teknik.Areas.API.Controllers |
||||
{ |
||||
public class APIController : DefaultController |
||||
{ |
||||
private TeknikEntities db = new TeknikEntities(); |
||||
|
||||
[AllowAnonymous] |
||||
public ActionResult Index() |
||||
{ |
||||
return Redirect(Url.SubRouteUrl("help", "Help.Topic", new { topic = "API" })); |
||||
} |
||||
|
||||
#region API v1 Actions
|
||||
[AllowAnonymous] |
||||
public ActionResult Index_v1() |
||||
{ |
||||
return Redirect(Url.SubRouteUrl("help", "Help.Topic", new { topic = "API" })); |
||||
} |
||||
|
||||
[HttpPost] |
||||
[AllowAnonymous] |
||||
public ActionResult Upload_v1(HttpPostedFileWrapper file, string contentType = null, bool encrypt = false, bool saveKey = false, string key = null, int keySize = 0, string iv = null, int blockSize = 0, bool genDeletionKey = false) |
||||
{ |
||||
if (file != null) |
||||
{ |
||||
if (file.ContentLength <= Config.UploadConfig.MaxUploadSize) |
||||
{ |
||||
// convert file to bytes
|
||||
byte[] fileData = null; |
||||
int contentLength = file.ContentLength; |
||||
using (var binaryReader = new BinaryReader(file.InputStream)) |
||||
{ |
||||
fileData = binaryReader.ReadBytes(file.ContentLength); |
||||
} |
||||
|
||||
// Need to grab the contentType if it's empty
|
||||
if (string.IsNullOrEmpty(contentType)) |
||||
{ |
||||
contentType = (string.IsNullOrEmpty(file.ContentType)) ? "application/octet-stream" : file.ContentType; |
||||
} |
||||
|
||||
// Initialize the key size and block size if empty
|
||||
if (keySize <= 0) |
||||
keySize = Config.UploadConfig.KeySize; |
||||
if (blockSize <= 0) |
||||
blockSize = Config.UploadConfig.BlockSize; |
||||
|
||||
byte[] data = null; |
||||
// If they want us to encrypt the file first, do that here
|
||||
if (encrypt) |
||||
{ |
||||
// Generate key and iv if empty
|
||||
if (string.IsNullOrEmpty(key)) |
||||
{ |
||||
key = Utility.RandomString(keySize); |
||||
} |
||||
if (string.IsNullOrEmpty(iv)) |
||||
{ |
||||
iv = Utility.RandomString(blockSize); |
||||
} |
||||
|
||||
data = AES.Encrypt(fileData, key, iv); |
||||
if (data == null || data.Length <= 0) |
||||
{ |
||||
return Json(new { error = "Unable to encrypt file" }); |
||||
} |
||||
} |
||||
|
||||
// Save the file data
|
||||
Upload.Models.Upload upload = Uploader.SaveFile((encrypt) ? data : fileData, contentType, contentLength, iv, key, keySize, blockSize); |
||||
|
||||
if (upload != null) |
||||
{ |
||||
// Save the key to the DB if they wanted it to be
|
||||
if (saveKey) |
||||
{ |
||||
upload.Key = key; |
||||
db.Entry(upload).State = EntityState.Modified; |
||||
db.SaveChanges(); |
||||
} |
||||
|
||||
// Generate delete key if asked to
|
||||
if (genDeletionKey) |
||||
{ |
||||
string delKey = Utility.RandomString(Config.UploadConfig.DeleteKeyLength); |
||||
upload.DeleteKey = delKey; |
||||
db.Entry(upload).State = EntityState.Modified; |
||||
db.SaveChanges(); |
||||
} |
||||
|
||||
// Pull all the information together
|
||||
var returnData = new |
||||
{ |
||||
name = upload.Url, |
||||
url = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url }), |
||||
contentType = contentType, |
||||
contentLength = contentLength, |
||||
key = key, |
||||
keySize = keySize, |
||||
iv = iv, |
||||
blockSize = blockSize, |
||||
deletionKey = upload.DeleteKey |
||||
|
||||
}; |
||||
return Json(new { result = new { name = upload.Url, url = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url }) } }, "text/plain"); |
||||
} |
||||
return Json(new { error = "Unable to save file" }); |
||||
} |
||||
else |
||||
{ |
||||
return Json(new { error = "File Too Large" }); |
||||
} |
||||
} |
||||
|
||||
return Json(new { error = "Invalid Upload Request" }); |
||||
} |
||||
#endregion
|
||||
} |
||||
} |
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0"?> |
||||
|
||||
<configuration> |
||||
<configSections> |
||||
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> |
||||
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> |
||||
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> |
||||
</sectionGroup> |
||||
</configSections> |
||||
|
||||
<system.web.webPages.razor> |
||||
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> |
||||
<pages pageBaseType="System.Web.Mvc.WebViewPage"> |
||||
<namespaces> |
||||
<add namespace="System.Web.Mvc" /> |
||||
<add namespace="System.Web.Mvc.Ajax" /> |
||||
<add namespace="System.Web.Mvc.Html" /> |
||||
<add namespace="System.Web.Routing" /> |
||||
<add namespace="System.Web.Optimization" /> |
||||
<add namespace="Teknik" /> |
||||
|
||||
</namespaces> |
||||
</pages> |
||||
</system.web.webPages.razor> |
||||
|
||||
<appSettings> |
||||
<add key="webpages:Enabled" value="false" /> |
||||
</appSettings> |
||||
|
||||
<system.webServer> |
||||
<handlers> |
||||
<remove name="BlockViewHandler"/> |
||||
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> |
||||
</handlers> |
||||
</system.webServer> |
||||
</configuration> |
Loading…
Reference in new issue