Skip to content

Instantly share code, notes, and snippets.

@ryankelley
Created March 20, 2012 20:51
Show Gist options
  • Save ryankelley/2141151 to your computer and use it in GitHub Desktop.
Save ryankelley/2141151 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Web;
using FubuCore;
using Rackspace.Cloudfiles;
namespace FieldBook.Core.FileUploads
{
public class FileSettings
{
public virtual string ContainerName { get; set; }
}
public interface IFileUploadService
{
bool ReceiveAndStoreUploadedFiles(Action<string, int> action, string uploadPath);
string ReceiveAndStoreUploadedFiles(HttpPostedFileBase file, string uploadPath);
UploadedFileInfo UploadFileToCloud(HttpPostedFileBase file, string uploadPath);
string DownloadFileFromCloud(string storedName, string savePath);
void DeleteFileFromCloud(string fileName);
string GetCloudUrl(string storedName);
Stream GetStreamFromCloud(string storedName);
}
public class FileUploadService : IFileUploadService
{
private readonly HttpContextBase _context;
private readonly FileSettings _fileSettings;
public FileUploadService(HttpContextBase context, FileSettings fileSettings)
{
_context = context;
_fileSettings = fileSettings;
_connection = null;
_tenantContainerName = "";
_client = new CF_Client();
}
#region IFileUploadService Members
private CF_Connection _connection;
private string _tenantContainerName;
private CF_Client _client;
private CF_Container _container;
private CF_Connection Connection
{
get
{
var userCredentials = new UserCredentials("user", "hash");
if (_connection == null)
{
_connection = new CF_Connection(userCredentials);
}
return _connection;
}
}
private string TenantContainerName
{
get
{
if (_tenantContainerName.IsEmpty())
{
_tenantContainerName = _fileSettings.ContainerName;
}
return _tenantContainerName;
}
}
private CF_Container Container
{
get
{
if (_container == null)
{
_container = new CF_Container(Connection, _client, TenantContainerName);
//_container.MakePublic();
}
return _container;
}
}
private string GetExtension(string filename)
{
var lastDecimal = filename.LastIndexOf('.');
return filename.Substring(lastDecimal, filename.Length - lastDecimal);
}
public string ReceiveAndStoreUploadedFiles(HttpPostedFileBase file, string uploadPath)
{
var newFileName = Path.GetRandomFileName() + GetExtension(file.FileName);
var savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + uploadPath + "/", newFileName);
file.SaveAs(savedFileName);
return savedFileName;
}
public UploadedFileInfo UploadFileToCloud(HttpPostedFileBase file, string uploadPath)
{
var tempDiskStoragePath = ReceiveAndStoreUploadedFiles(file, uploadPath);
var fileInfo = new FileInfo(tempDiskStoragePath);
var storedName = Guid.NewGuid().ToString() + GetExtension(file.FileName);
var obj = new CF_Object(Connection, Container, _client, storedName);
var stream = fileInfo.Open(FileMode.Open);
obj.Write(stream);
//obj.WriteFromFile(fileInfo.FullName);
//Connection.PutStorageItem(TenantContainerName, fileInfo.Open(FileMode.Open), file.FileName);
File.Delete(tempDiskStoragePath);
return new UploadedFileInfo(obj.CdnUri.AbsoluteUri, storedName);
}
public Stream GetStreamFromCloud(string storedName)
{
var obj = Container.GetObject(storedName);
return obj.Read();
}
public string GetCloudUrl(string storedName)
{
var item = Container.GetObject(storedName);
return item.CdnUri.AbsoluteUri;
}
public string DownloadFileFromCloud(string storedName, string savePath)
{
var extension = GetExtension(storedName);
var newFileName = Path.GetRandomFileName() + extension;
var savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + savePath + "/", newFileName);
var obj = Container.GetObject(storedName);
obj.SaveToFile(savedFileName);
return newFileName;
}
public void DeleteFileFromCloud(string fileName)
{
Container.DeleteObject(fileName);
}
public bool ReceiveAndStoreUploadedFiles(Action<string, int> action, string uploadPath)
{
for (int i = 0; i < _context.Request.Files.Count; ++i)
{
var hpf = _context.Request.Files[i];
if (hpf.ContentLength == 0)
continue;
var extension = GetExtension(hpf.FileName);
var savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + uploadPath + "/", Path.GetRandomFileName() + extension);
hpf.SaveAs(savedFileName);
extension = GetExtension(savedFileName);
var newFileName = Path.GetRandomFileName() + extension;
var newSavedFile = AppDomain.CurrentDomain.BaseDirectory + uploadPath + "/" + newFileName;
var onlineFilePath = "/" + uploadPath + "/" + newFileName;
if (File.Exists(savedFileName))
File.Move(savedFileName, newSavedFile);
action(onlineFilePath, hpf.ContentLength);
}
return true;
}
#endregion
}
public class UploadedFileInfo
{
public UploadedFileInfo(string url, string storedName)
{
Url = url;
StoredName = storedName;
}
public virtual string Url { get; set; }
public virtual string StoredName { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment