The next generation of the Teknik Services. Written in ASP.NET.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

58 lines
2.1 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Teknik.Configuration;
using Teknik.Models;
namespace Teknik.Areas.Upload
{
public static class Uploader
{
public static Models.Upload SaveFile(HttpPostedFileWrapper file, string contentType)
{
return SaveFile(file, contentType, null, null);
}
public static Models.Upload SaveFile(HttpPostedFileWrapper file, string contentType, string iv)
{
return SaveFile(file, contentType, iv, null);
}
public static Models.Upload SaveFile(HttpPostedFileWrapper file, string contentType, string iv, string key)
{
Config config = Config.Load();
TeknikEntities db = new TeknikEntities();
// Generate a unique file name that does not currently exist
string fileName = Utility.GenerateUniqueFileName(config.UploadConfig.UploadDirectory, config.UploadConfig.FileExtension, 10);
// once we have the filename, lets save the file
file.SaveAs(fileName);
// Generate a unique url
string extension = (config.UploadConfig.IncludeExtension) ? Utility.GetDefaultExtension(contentType) : string.Empty;
string url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
while (db.Uploads.Where(u => u.Url == url).FirstOrDefault() != null)
{
url = Utility.RandomString(config.UploadConfig.UrlLength) + extension;
}
// Now we need to update the database with the new upload information
Models.Upload upload = db.Uploads.Create();
upload.DateUploaded = DateTime.Now;
upload.Url = url;
upload.FileName = fileName;
upload.ContentType = (!string.IsNullOrEmpty(contentType)) ? contentType : "application/octet-stream";
upload.ContentLength = file.ContentLength;
upload.Key = key;
upload.IV = iv;
db.Uploads.Add(upload);
db.SaveChanges();
return upload;
}
}
}