Skip to content

Instantly share code, notes, and snippets.

@angusbreno
Last active January 19, 2018 12:53
Show Gist options
  • Save angusbreno/92e706cfb906c7250dcb2292efd8d521 to your computer and use it in GitHub Desktop.
Save angusbreno/92e706cfb906c7250dcb2292efd8d521 to your computer and use it in GitHub Desktop.
UploadFile WebAPI - Server
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
namespace BiFrost.Service.Web.Services
{
public class AzureBlobManagerService : IAzureBlobManagerService
{
public void Initialize()
{
}
public CloudBlobContainer GetContainer(string containerName)
{
var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("CloudStorageConnectionString"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
var permissions = container.GetPermissions();
if (permissions.PublicAccess == BlobContainerPublicAccessType.Off)
{
permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
container.SetPermissions(permissions);
}
return container;
}
}
public interface IAzureBlobManagerService
{
CloudBlobContainer GetContainer(string containerName);
}
}
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace BiFrost.Service.Web.Framework.WebAPI.Providers
{
public class BlobStorageProvider : MultipartFileStreamProvider
{
private readonly CloudBlobContainer _container;
public static string GetTempPath()
{
var path = Path.Combine(Path.GetTempPath(), "BlobStorageProviderTemp");
Directory.CreateDirectory(path);
return path;
}
public BlobStorageProvider(CloudBlobContainer container)
: base(GetTempPath())
{
_container = container;
Urls = new List<string>();
}
public IList<string> Urls { get; private set; }
public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
{
var name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? headers.ContentDisposition.FileName : "NoName";
name = name.Replace("\"", string.Empty); //this is here because Chrome submits files in quotation marks which get treated as part of the filename and get escaped
var s = Guid.NewGuid().ToString() + (Path.GetExtension(name));
return s;
}
public override Task ExecutePostProcessingAsync()
{
foreach (var file in FileData)
{
string fileName = Path.GetFileName(file.LocalFileName.Trim('"'));
var blob = _container.GetBlockBlobReference(fileName);
try
{
blob.Properties.ContentType = System.Web.MimeMapping.GetMimeMapping(fileName);
}
catch (System.Exception) { }
using (var stream = File.OpenRead(file.LocalFileName))
{
blob.UploadFromStream(stream);
}
File.Delete(file.LocalFileName);
Urls.Add(blob.Uri.AbsoluteUri);
}
return base.ExecutePostProcessingAsync();
}
}
}
using BiFrost.DataInfrastructure.ORM.Contexts;
using BiFrost.Service.Web.Framework.WebAPI.Extensions;
using BiFrost.Service.Web.Framework.WebAPI.Providers;
using BiFrost.Service.Web.Services;
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
namespace BiFrost.Service.Web.Controllers.Core
{
[RoutePrefix("api/Storage")]
public class StorageController : BiFrostCoreBaseApiController
{
[Route("UploadFile")]
[HttpPost]
public async Task<object> UploadFile()
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
}
var provider = new BlobStorageProvider(this.GetBiFrostService<AzureBlobManagerService>().GetContainer("files"));
await Request.Content.ReadAsMultipartAsync(provider);
return provider.Urls;
}
}
}
@angusbreno
Copy link
Author

Lembrar de colar a interface na implemntacao antes de pedir pra DI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment