Browse Source

Fixed error checking for upload APIv1 and some example text.

tags/2.0.3
Teknikode 4 years ago
parent
commit
486ee2c2b1

+ 4
- 4
Teknik/Areas/API/APIAreaRegistration.cs View File

@@ -20,14 +20,14 @@ namespace Teknik.Areas.API
"APIv1.Index", // Route name
"dev",
"API/v1", // URL with parameters
new { controller = "API", action = "Index" }, // Parameter defaults
new { controller = "APIv1", action = "Index" }, // Parameter defaults
new[] { typeof(Controllers.APIv1Controller).Namespace }
);
context.MapSubdomainRoute(
"APIv1.Index", // Route name
"api",
"v1", // URL with parameters
new { controller = "API", action = "Index" }, // Parameter defaults
new { controller = "APIv1", action = "Index" }, // Parameter defaults
new[] { typeof(Controllers.APIv1Controller).Namespace }
);
// Uploads
@@ -35,14 +35,14 @@ namespace Teknik.Areas.API
"APIv1.Upload", // Route name
"dev",
"API/v1/Upload", // URL with parameters
new { controller = "API", action = "Upload" }, // Parameter defaults
new { controller = "APIv1", action = "Upload" }, // Parameter defaults
new[] { typeof(Controllers.APIv1Controller).Namespace }
);
context.MapSubdomainRoute(
"APIv1.Upload", // Route name
"api",
"v1/Upload", // URL with parameters
new { controller = "API", action = "Upload" }, // Parameter defaults
new { controller = "APIv1", action = "Upload" }, // Parameter defaults
new[] { typeof(Controllers.APIv1Controller).Namespace }
);
#endregion

+ 73
- 74
Teknik/Areas/API/Controllers/APIv1Controller.cs View File

@@ -26,98 +26,97 @@ namespace Teknik.Areas.API.Controllers
[AllowAnonymous]
public ActionResult Upload(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)
try {
if (file != null)
{
// 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))
if (file.ContentLength <= Config.UploadConfig.MaxUploadSize)
{
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))
// convert file to bytes
byte[] fileData = null;
int contentLength = file.ContentLength;
using (var binaryReader = new BinaryReader(file.InputStream))
{
iv = Utility.RandomString(blockSize);
fileData = binaryReader.ReadBytes(file.ContentLength);
}

data = AES.Encrypt(fileData, key, iv);
if (data == null || data.Length <= 0)
// Need to grab the contentType if it's empty
if (string.IsNullOrEmpty(contentType))
{
return Json(new { error = "Unable to encrypt file" });
contentType = (string.IsNullOrEmpty(file.ContentType)) ? "application/octet-stream" : file.ContentType;
}
}

// Save the file data
Upload.Models.Upload upload = Uploader.SaveFile((encrypt) ? data : fileData, contentType, contentLength, iv, key, keySize, blockSize);
// Initialize the key size and block size if empty
if (keySize <= 0)
keySize = Config.UploadConfig.KeySize;
if (blockSize <= 0)
blockSize = Config.UploadConfig.BlockSize;

if (upload != null)
{
// Save the key to the DB if they wanted it to be
if (saveKey)
byte[] data = null;
// If they want us to encrypt the file first, do that here
if (encrypt)
{
upload.Key = key;
db.Entry(upload).State = EntityState.Modified;
db.SaveChanges();
}
// Generate key and iv if empty
if (string.IsNullOrEmpty(key))
{
key = Utility.RandomString(keySize / 8);
}
if (string.IsNullOrEmpty(iv))
{
iv = Utility.RandomString(blockSize / 8);
}

// 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();
data = AES.Encrypt(fileData, key, iv);
if (data == null || data.Length <= 0)
{
return Json(new { error = new { message = "Unable to encrypt file" } });
}
}

// Pull all the information together
var returnData = new
// Save the file data
Upload.Models.Upload upload = Uploader.SaveFile((encrypt) ? data : fileData, contentType, contentLength, iv, (saveKey) ? key : null, keySize, blockSize);

if (upload != null)
{
url = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url }),
fileName = upload.Url,
contentType = contentType,
contentLength = contentLength,
key = key,
keySize = keySize,
iv = iv,
blockSize = blockSize,
deletionKey = upload.DeleteKey
// 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
string fullUrl = Url.SubRouteUrl("upload", "Upload.Download", new { file = upload.Url });
var returnData = new
{
url = (saveKey) ? fullUrl : fullUrl + "#" + key,
fileName = upload.Url,
contentType = contentType,
contentLength = contentLength,
key = key,
keySize = keySize,
iv = iv,
blockSize = blockSize,
deletionKey = upload.DeleteKey

};
return Json(new { result = returnData });
};
return Json(new { result = returnData });
}
return Json(new { error = new { message = "Unable to save file" } });
}
else
{
return Json(new { error = new { message = "File Too Large" } });
}
return Json(new { error = "Unable to save file" });
}
else
{
return Json(new { error = "File Too Large" });
}
}

return Json(new { error = "Invalid Upload Request" });
return Json(new { error = new { message = "Invalid Upload Request" } });
}
catch(Exception ex)
{
return Json(new { error = new { message = "Exception: " + ex.Message } });
}
}
}
}

+ 2
- 2
Teknik/Areas/Help/Views/Help/API/v1/Upload.cshtml View File

@@ -135,7 +135,7 @@
</tr>
<tr>
<td>
<code>getDeletionKey</code>
<code>genDeletionKey</code>
</td>
<td>
<code>bool</code>
@@ -265,7 +265,7 @@
</tbody>
</table>
<h4>Example</h4>
<pre><code>$ curl -F "getDeletionKey=true" -F "encrypt=yes" -F "file=@("@")image.png" @Url.SubRouteUrl("api", "APIv1.Upload")</code></pre>
<pre><code>$ curl -F "genDeletionKey=true" -F "encrypt=true" -F "file=@("@")image.png" @Url.SubRouteUrl("api", "APIv1.Upload")</code></pre>
<p>
This will upload the file <var>image.png</var>, encrypt it, and then generate a deletion key.
</p>

Loading…
Cancel
Save