Example #1
0
        /// <summary>
        /// Stores the file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <returns></returns>
        private HiveId StoreFile(HttpPostedFileBase file)
        {
            //saving file
            var hive = BackOfficeRequestContext.Application.Hive.GetWriter <IFileStore>(new Uri("storage://file-uploader"));

            //create new media item
            //currently code is in here but should be shared with upload prop editor

            var mediaId = Guid.NewGuid();

            // Open a new unit of work to write the file
            using (var uow = hive.Create())
            {
                // Create main file
                var f = new File
                {
                    RootedPath = mediaId.ToString("N") + "/" + file.FileName.Replace(" ", "")
                };
                //.ToString("N")
                var stream = file.InputStream;
                if (stream.CanRead && stream.CanSeek)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    using (var mem = new MemoryStream())
                    {
                        stream.CopyTo(mem);
                        f.ContentBytes = mem.ToArray();
                    }
                }

                uow.Repositories.AddOrUpdate(f);

                //Create thumbnails (TODO: Need to encapsulate this so it can be reused in other places?)
                if (f.IsImage())
                {
                    var img = Image.FromStream(file.InputStream);

                    // Create default thumbnail
                    CreateThumbnail(uow, f, img, mediaId.ToString("N"), 100);
                    //// Create additional thumbnails
                }

                uow.Complete();

                return(f.Id);
            }
        }