1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Added caching to the uploads if not getting by range.

This commit is contained in:
Uncled1023 2016-05-26 16:01:41 -07:00
parent 3f497b3de1
commit ff69c5080e

View File

@ -164,99 +164,121 @@ namespace Teknik.Areas.Upload.Controllers
} }
else // We have the key, so that means server side decryption else // We have the key, so that means server side decryption
{ {
string subDir = upload.FileName[0].ToString(); // Check to see if they have a cache
string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, subDir, upload.FileName); bool isCached = !string.IsNullOrEmpty(Request.Headers["If-Modified-Since"]);
long startByte = 0;
long endByte = upload.ContentLength - 1; if (isCached)
long length = upload.ContentLength;
if (System.IO.File.Exists(filePath))
{ {
// Read in the file // The file is cached, let's just 304 this
byte[] data = System.IO.File.ReadAllBytes(filePath); Response.StatusCode = 304;
// If the IV is set, and Key is set, then decrypt it Response.StatusDescription = "Not Modified";
if (!string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV)) Response.AddHeader("Content-Length", "0");
return Content(string.Empty);
}
else
{
string subDir = upload.FileName[0].ToString();
string filePath = Path.Combine(Config.UploadConfig.UploadDirectory, subDir, upload.FileName);
long startByte = 0;
long endByte = upload.ContentLength - 1;
long length = upload.ContentLength;
if (System.IO.File.Exists(filePath))
{ {
// Decrypt the data // Read in the file
data = AES.Decrypt(data, upload.Key, upload.IV); byte[] data = System.IO.File.ReadAllBytes(filePath);
} // If the IV is set, and Key is set, then decrypt it
if (!string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV))
// We accept ranges
Response.AddHeader("Accept-Ranges", "0-" + upload.ContentLength);
// check to see if we need to pass a specified range
if (!String.IsNullOrEmpty(Request.ServerVariables["HTTP_RANGE"]))
{
long anotherStart = startByte;
long anotherEnd = endByte;
string[] arr_split = Request.ServerVariables["HTTP_RANGE"].Split(new char[] { '=' });
string range = arr_split[1];
// Make sure the client hasn't sent us a multibyte range
if (range.IndexOf(",") > -1)
{ {
// (?) Shoud this be issued here, or should the first // Decrypt the data
// range be used? Or should the header be ignored and data = AES.Decrypt(data, upload.Key, upload.IV);
// we output the whole content?
Response.AddHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + upload.ContentLength);
throw new HttpException(416, "Requested Range Not Satisfiable");
} }
// If the range starts with an '-' we start from the beginning // We accept ranges
// If not, we forward the file pointer Response.AddHeader("Accept-Ranges", "0-" + upload.ContentLength);
// And make sure to get the end byte if spesified
if (range.StartsWith("-")) // check to see if we need to pass a specified range
if (!String.IsNullOrEmpty(Request.ServerVariables["HTTP_RANGE"]))
{ {
// The n-number of the last bytes is requested long anotherStart = startByte;
anotherStart = startByte - Convert.ToInt64(range.Substring(1)); long anotherEnd = endByte;
string[] arr_split = Request.ServerVariables["HTTP_RANGE"].Split(new char[] { '=' });
string range = arr_split[1];
// Make sure the client hasn't sent us a multibyte range
if (range.IndexOf(",") > -1)
{
// (?) Shoud this be issued here, or should the first
// range be used? Or should the header be ignored and
// we output the whole content?
Response.AddHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + upload.ContentLength);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
// If the range starts with an '-' we start from the beginning
// If not, we forward the file pointer
// And make sure to get the end byte if spesified
if (range.StartsWith("-"))
{
// The n-number of the last bytes is requested
anotherStart = startByte - Convert.ToInt64(range.Substring(1));
}
else
{
arr_split = range.Split(new char[] { '-' });
anotherStart = Convert.ToInt64(arr_split[0]);
long temp = 0;
anotherEnd = (arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : upload.ContentLength;
}
/* Check the range and make sure it's treated according to the specs.
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
*/
// End bytes can not be larger than $end.
anotherEnd = (anotherEnd > endByte) ? endByte : anotherEnd;
// Validate the requested range and return an error if it's not correct.
if (anotherStart > anotherEnd || anotherStart > upload.ContentLength - 1 || anotherEnd >= upload.ContentLength)
{
Response.AddHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + upload.ContentLength);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
startByte = anotherStart;
endByte = anotherEnd;
length = endByte - startByte + 1; // Calculate new content length
// grab the portion of the data we want
byte[] dataRange = new byte[length];
Array.Copy(data, startByte, dataRange, 0, length);
data = dataRange;
// Ranges are response of 206
Response.StatusCode = 206;
} }
else else
{ {
arr_split = range.Split(new char[] { '-' }); // No content range requested, so we can cache it.
anotherStart = Convert.ToInt64(arr_split[0]); Response.Cache.SetCacheability(HttpCacheability.Public);
long temp = 0; Response.Cache.SetMaxAge(new TimeSpan(365, 0, 0, 0));
anotherEnd = (arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : upload.ContentLength; Response.Cache.SetLastModified(upload.DateUploaded);
} }
/* Check the range and make sure it's treated according to the specs. // Notify the client the byte range we'll be outputting
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html Response.AddHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + upload.ContentLength);
*/ Response.AddHeader("Content-Length", length.ToString());
// End bytes can not be larger than $end.
anotherEnd = (anotherEnd > endByte) ? endByte : anotherEnd; // Create content disposition
// Validate the requested range and return an error if it's not correct. var cd = new System.Net.Mime.ContentDisposition
if (anotherStart > anotherEnd || anotherStart > upload.ContentLength - 1 || anotherEnd >= upload.ContentLength)
{ {
FileName = upload.Url,
Inline = true
};
Response.AddHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + upload.ContentLength); Response.AppendHeader("Content-Disposition", cd.ToString());
throw new HttpException(416, "Requested Range Not Satisfiable");
}
startByte = anotherStart;
endByte = anotherEnd;
length = endByte - startByte + 1; // Calculate new content length return File(data, upload.ContentType);
// grab the portion of the data we want
byte[] dataRange = new byte[length];
Array.Copy(data, startByte, dataRange, 0, length);
data = dataRange;
// Ranges are response of 206
Response.StatusCode = 206;
} }
// Notify the client the byte range we'll be outputting
Response.AddHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + upload.ContentLength);
Response.AddHeader("Content-Length", length.ToString());
// Create content disposition
var cd = new System.Net.Mime.ContentDisposition
{
FileName = upload.Url,
Inline = true
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(data, upload.ContentType);
} }
} }
} }