Beispiel #1
0
        public IActionResult UploadFile(List <IFormFile> files) // async Task<>
        {
            if (files.Count == 0)
            {
                return(BadRequest(new { message = "Zero files" }));
            }
            try
            {
                foreach (var formFile in files)
                {
                    if (formFile.Length > 0)
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            int uid = int.Parse(this.User.FindFirstValue(ClaimTypes.Name));

                            string fname = formFile.FileName;
                            formFile.CopyTo(memoryStream);
                            //await formFile.CopyToAsync(memoryStream);
                            byte[] file = memoryStream.ToArray();

                            _uploadService.Create(fname, file, uid);
                        }
                    }
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }
        /// <summary>
        /// Called when file uploaded posted to this controller.
        /// Credit: http://stackoverflow.com/questions/10320232/how-to-accept-a-file-post-asp-net-mvc-4-webapi (Mike Wasson's answer) (Old)
        /// Credit: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads (New)
        /// </summary>
        /// <param name="files">Uploaded files.</param>
        /// <returns>An HTTP action result.</returns>
        public async Task <IActionResult> Post(List <IFormFile> files)
        {
            // Get tenant identifier
            long tenantId = _authenticationService.TenantId;

            // Process each file
            List <long> uploadIds = new List <long>();

            foreach (IFormFile file in files)
            {
                // Get file
                string name        = file.FileName.Trim('\"');
                string contentType = file.ContentType;
                byte[] content;
                using (MemoryStream ms = new MemoryStream())
                {
                    await file.CopyToAsync(ms);

                    content = ms.ToArray();
                }

                // Create upload
                CreateUploadModel model = new CreateUploadModel
                {
                    TenantId    = tenantId,
                    Name        = name,
                    Content     = content,
                    ContentType = contentType
                };
                long uploadId = _uploadService.Create(model);
                uploadIds.Add(uploadId);
            }

            // Convert upload IDs to comma separated string and return this
            string responseContent = string.Join(",", uploadIds);

            // Everything completed successfully - return list of newly allocated upload identifiers
            return(Ok(responseContent));
        }
Beispiel #3
0
        /// <summary>
        /// From a single uploaded file, creates thumbnail, preview and original images in underlying storage that may be associated with a page.
        /// </summary>
        /// <param name="tenantId">Website that page belongs to.</param>
        /// <param name="masterPageId">Master page containing image upload rules.</param>
        /// <param name="model">Image upload.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        /// <returns>Identifiers of newly created thumbnail, preview and source images.</returns>
        public ImageUploadIds PrepareImages(long tenantId, long masterPageId, CreateUploadModel model, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            // Begin work
            try
            {
                // Check that master page allows page images and that uploaded content is valid image
                ValidatePrepareImagesResult result = _pageValidator.ValidatePrepareImages(tenantId, masterPageId, model);

                // Create thumbnail model
                ResizeInfo thumbnailResizeInfo = new ResizeInfo
                {
                    Width      = result.MasterPage.ThumbnailImageWidth.Value,
                    Height     = result.MasterPage.ThumbnailImageHeight.Value,
                    ResizeMode = result.MasterPage.ThumbnailImageResizeMode.Value
                };
                byte[]            thumbnailContent = _imageAnalysisService.ResizeImage(model.Content, thumbnailResizeInfo);
                CreateUploadModel thumbnailModel   = new CreateUploadModel
                {
                    Content     = thumbnailContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create preview model
                ResizeInfo previewResizeInfo = new ResizeInfo
                {
                    Width      = result.MasterPage.PreviewImageWidth.Value,
                    Height     = result.MasterPage.PreviewImageHeight.Value,
                    ResizeMode = result.MasterPage.PreviewImageResizeMode.Value
                };
                byte[]            previewContent = _imageAnalysisService.ResizeImage(model.Content, previewResizeInfo);
                CreateUploadModel previewModel   = new CreateUploadModel
                {
                    Content     = previewContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create uploads for thumbnail, preview and original image
                long thumbnailImageUploadId = _uploadService.Create(thumbnailModel, unitOfWork ?? localUnitOfWork);
                long previewImageUploadId   = _uploadService.Create(previewModel, unitOfWork ?? localUnitOfWork);
                long imageUploadId          = _uploadService.Create(model, unitOfWork ?? localUnitOfWork);

                // Commit work if local unit of work in place and return result
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(new ImageUploadIds {
                    ThumbnailImageUploadId = thumbnailImageUploadId, PreviewImageUploadId = previewImageUploadId, ImageUploadId = imageUploadId
                });
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }
Beispiel #4
0
        public long PrepareImages(long tenantId, long elementId, CreateUploadModel model, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            // Begin work
            try
            {
                // Check that uploaded content is valid image
                System.Drawing.Size imageSize = _htmlValidator.ValidatePrepareImages(tenantId, elementId, model);

                // Get HTML settings (does not have to be within unit of work)
                HtmlSettings htmlSettings = (HtmlSettings)New(tenantId);
                htmlSettings.ElementId = elementId;
                Read(htmlSettings);

                // Create thumbnail model
                byte[] thumbnailContent = model.Content;
                if (imageSize.Width > htmlSettings.ThumbnailImageWidth || imageSize.Height > htmlSettings.ThumbnailImageHeight)
                {
                    ResizeInfo thumbnailResizeInfo = new ResizeInfo
                    {
                        Width      = htmlSettings.ThumbnailImageWidth,
                        Height     = htmlSettings.ThumbnailImageHeight,
                        ResizeMode = htmlSettings.ThumbnailImageResizeMode
                    };
                    thumbnailContent = _imageAnalysisService.ResizeImage(model.Content, thumbnailResizeInfo);
                }
                CreateUploadModel thumbnailModel = new CreateUploadModel
                {
                    Content     = thumbnailContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create preview model
                byte[] previewContent = model.Content;
                if (imageSize.Width > htmlSettings.PreviewImageWidth || imageSize.Height > htmlSettings.PreviewImageHeight)
                {
                    ResizeInfo previewResizeInfo = new ResizeInfo
                    {
                        Width      = htmlSettings.PreviewImageWidth,
                        Height     = htmlSettings.PreviewImageHeight,
                        ResizeMode = htmlSettings.PreviewImageResizeMode
                    };
                    previewContent = _imageAnalysisService.ResizeImage(model.Content, previewResizeInfo);
                }
                CreateUploadModel previewModel = new CreateUploadModel
                {
                    Content     = previewContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create uncommitted uploads for thumbnail, preview and original image
                long thumbnailImageUploadId = _uploadService.Create(thumbnailModel, unitOfWork ?? localUnitOfWork);
                long previewImageUploadId   = _uploadService.Create(previewModel, unitOfWork ?? localUnitOfWork);
                long imageUploadId          = _uploadService.Create(model, unitOfWork ?? localUnitOfWork);

                // Commit uploads
                _uploadService.Commit(tenantId, thumbnailImageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);
                _uploadService.Commit(tenantId, previewImageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);
                _uploadService.Commit(tenantId, imageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);

                // Create HTML image, recording upload IDs of newly created images
                HtmlUpload upload = new HtmlUpload
                {
                    TenantId               = tenantId,
                    ElementId              = elementId,
                    ImageTenantId          = tenantId,
                    ThumbnailImageUploadId = thumbnailImageUploadId,
                    PreviewImageUploadId   = previewImageUploadId,
                    ImageUploadId          = imageUploadId
                };
                long htmlImageId = _htmlRepository.CreateUpload(upload, unitOfWork ?? localUnitOfWork);

                // Commit work if local unit of work in place and return result
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(htmlImageId);
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }