Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c8a17b
commit 54eb7fd
Showing
22 changed files
with
682 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using System.Web.Http; | ||
using System.Web.Http.Description; | ||
using Weavy.Core; | ||
using Weavy.Core.Models; | ||
using Weavy.Core.Services; | ||
using Weavy.Web.Api.Controllers; | ||
using Weavy.Web.Api.Models; | ||
using Weavy.Web.Api.Streamers; | ||
|
||
namespace Weavy.Areas.Api.Controllers { | ||
|
||
/// <summary> | ||
/// Api controller for manipulating Blobs. | ||
/// </summary> | ||
[RoutePrefix("api")] | ||
public class BlobsController : WeavyApiController { | ||
|
||
/// <summary> | ||
/// Uploads new blob(s). Use multipart/form-data for the request format. | ||
/// After upload the blobs can be used for setting avatars or as references when creating attachments and/or files. | ||
/// </summary> | ||
/// <returns>The uploaded blobs(s).</returns> | ||
[HttpPost] | ||
[Route("blobs")] | ||
public async Task<ScrollableList<Blob>> Upload() { | ||
// check if the request contains multipart/form-data. | ||
if (!Request.Content.IsMimeMultipartContent()) { | ||
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); | ||
} | ||
|
||
// write uploaded files to local disk cache | ||
var provider = await Request.Content.ReadAsMultipartAsync(new BlobMultipartFormDataRemoteStreamProvider()); | ||
|
||
// iterate over the uploaded files and store them as blobs in the database | ||
List<Blob> blobs = new List<Blob>(); | ||
foreach (var data in provider.FileData) { | ||
var blob = provider.GetBlob(data); | ||
if (blob != null) { | ||
try { | ||
blob = BlobService.Insert(blob, System.IO.File.OpenRead(data.Location)); | ||
blobs.Add(blob); | ||
} catch { | ||
ThrowResponseException(HttpStatusCode.InternalServerError, "Failed to upload blobs"); | ||
} | ||
} | ||
} | ||
return new ScrollableList<Blob>(blobs, null, null, blobs.Count(), Request.RequestUri); | ||
} | ||
} | ||
} |
Oops, something went wrong.