BrickPile.UI.Areas.UI.Controllers.AssetController.Post C# (CSharp) Method

Post() public method

Posts this instance.
public Post ( ) : Task>
return Task>
        public Task<IEnumerable<Asset>> Post()
        {
            if (!Request.Content.IsMimeMultipartContent()) {

                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));

            }

            var task = Request.Content.ReadAsMultipartAsync(new MultipartMemoryStreamProvider()).ContinueWith(t => {

                using (var session = _store.OpenSession()) {

                    var virtualPathProvider = HostingEnvironment.VirtualPathProvider as CommonVirtualPathProvider;

                    if (t.IsFaulted || t.IsCanceled || virtualPathProvider == null) {
                        throw new HttpResponseException(HttpStatusCode.InternalServerError);
                    }

                    var asset = t.Result.Contents.Select(httpContent => {
                        // Read the stream
                        var stream = httpContent.ReadAsStreamAsync().Result;
                        var length = stream.Length;

                        // Get root directory of the current virtual path provider
                        var virtualDirectory =
                            virtualPathProvider.GetDirectory(virtualPathProvider.VirtualPathRoot) as
                                CommonVirtualDirectory;

                        if (virtualDirectory == null) {
                            throw new HttpResponseException(HttpStatusCode.InternalServerError);
                        }

                        // Set the name and if not present add some bogus name
                        var name = !string.IsNullOrWhiteSpace(httpContent.Headers.ContentDisposition.FileName)
                            ? httpContent.Headers.ContentDisposition.FileName
                            : "NoName";

                        // Clean up the name
                        name = name.Replace("\"", string.Empty);

                        // Create a new name for the file stored in the vpp
                        var uniqueFileName = Guid.NewGuid().ToString("n");

                        // Create the file in current directory
                        var virtualFile =
                            virtualDirectory.CreateFile(string.Format("{0}{1}", uniqueFileName,
                                VirtualPathUtility.GetExtension(name)));

                        // Write the file to the current storage
                        using (var s = virtualFile.Open(FileMode.Create)) {
                            var bytesInStream = new byte[stream.Length];
                            stream.Read(bytesInStream, 0, bytesInStream.Length);
                            s.Write(bytesInStream, 0, bytesInStream.Length);
                        }

                        Asset file;
                        if (httpContent.Headers.ContentType.MediaType.Contains("image")) {
                            file = new Image();
                            using (var image = System.Drawing.Image.FromStream(stream, false, false)) {
                                ((Image) file).Width = image.Width;
                                ((Image) file).Height = image.Height;
                            }
                            var mediumThumbnail = new WebImage(stream).Resize(111, 101).Crop(1, 1);
                            file.Thumbnail = mediumThumbnail.GetBytes();
                        }
                        else if (httpContent.Headers.ContentType.MediaType.Contains("video")) {
                            var icon =
                                new WebImage(HttpContext.Current.Server.MapPath("~/areas/ui/content/images/document.png"));
                            file = new Video {Thumbnail = icon.GetBytes()};
                        }
                        else if (httpContent.Headers.ContentType.MediaType.Contains("audio")) {
                            var icon =
                                new WebImage(HttpContext.Current.Server.MapPath("~/areas/ui/content/images/document.png"));
                            file = new Audio {Thumbnail = icon.GetBytes()};
                        }
                        else {
                            var icon =
                                new WebImage(HttpContext.Current.Server.MapPath("~/areas/ui/content/images/document.png"));
                            file = new Document {Thumbnail = icon.GetBytes()};
                        }

                        file.Name = name;
                        file.ContentType = httpContent.Headers.ContentType.MediaType;
                        file.ContentLength = length;
                        file.DateUploaded = DateTime.Now;
                        file.VirtualPath = virtualFile.VirtualPath;
                        file.Url = virtualFile.Url;

                        session.Store(file);
                        session.SaveChanges();

                        return file;
                    });

                    return asset;
                }

            });

            return task;
        }