Exemplo n.º 1
0
        public async Task <IActionResult> OnPost(IFormFile image, IFormFile template, string guid, string title, string keywords, string description)
        {
            Template temp = this.context.Set <Template>().Where(t => t.DocumentLink.Equals(guid)).FirstOrDefault();

            if (temp == null)
            {
                return(Redirect("/templates"));
            }

            string toDeleteImage = null, toDeleteDocument = null;

            if (image != null)
            {
                LocalFile localFile = LocalFile.Create(image);
                await azureFileController.UploadFile(FileType.Images, localFile.LocalPath, localFile.Stream);

                toDeleteImage         = temp.PreviewImageLink;
                temp.PreviewImageLink = Path.GetFileName(localFile.LocalPath);
            }

            if (template != null)
            {
                LocalFile localFile = LocalFile.Create(template);
                await azureFileController.UploadFile(FileType.Templates, localFile.LocalPath, localFile.Stream);

                toDeleteDocument  = temp.DocumentLink;
                temp.DocumentLink = Path.GetFileName(localFile.LocalPath);
            }

            temp.Title       = title;
            temp.Description = description;
            temp.Keywords    = keywords;
            await context.SaveChangesAsync();

            if (!string.IsNullOrEmpty(toDeleteDocument))
            {
                try
                {
                    await azureFileController.DeleteFile(FileType.Templates, toDeleteDocument);
                } catch (Exception e) {
                    System.Diagnostics.Trace.TraceError(string.Format("Deleting old Document failed. Guid: {0}. Stack: {1}", toDeleteDocument, e.StackTrace));
                }
            }

            if (!string.IsNullOrEmpty(toDeleteImage))
            {
                try
                {
                    await azureFileController.DeleteFile(FileType.Images, toDeleteImage);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.TraceError(string.Format("Deleting old Image failed. Guid: {0}. Stack: {1}", toDeleteImage, e.StackTrace));
                }
            }

            return(OnGet(temp.ID.ToString()));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> OnPost(IFormFile image, IFormFile template, string title, string keywords, string description)
        {
            string userEmail = this.HttpContext.User.Identity.Name;

            if (string.IsNullOrEmpty(userEmail))
            {
                return(Redirect("~/login"));
            }

            this.CurrentUser = _context.Set <User>().Where(entry => entry.Email.Equals(userEmail)).FirstOrDefault();
            if (this.CurrentUser == null)
            {
                return(Redirect("~/login"));
            }
            else
            {
                if (!acceptableImageExtensions.Contains(Path.GetExtension(image.FileName)))
                {
                    this.Error = "Only JPG and PNG images can be uploaded at this time.";
                    return(Page());
                }

                if (!acceptableTemplateExtensions.Contains(Path.GetExtension(template.FileName)))
                {
                    this.Error = "Only DOCX files can be uploaded at this time.";
                    return(Page());
                }

                Template toAdd = new Template()
                {
                    UploadDate  = DateTime.UtcNow,
                    Title       = title,
                    UserID      = this.CurrentUser.ID,
                    Keywords    = keywords,
                    Description = description
                };

                LocalFile localFile = LocalFile.Create(template);
                try
                {
                    await azureFileController.UploadFile(FileType.Templates, localFile.LocalPath, localFile.Stream);
                }
                catch (Exception e)
                {
                    this.Error = "Something bad happened during uploading... Please try again later.";
                    System.Diagnostics.Trace.TraceError("Error during uploading:" + e.ToString());
                    await azureFileController.DeleteFile(FileType.Templates, Path.GetFileName(localFile.LocalPath));

                    return(Page());
                }
                toAdd.DocumentLink = Path.GetFileName(localFile.LocalPath);

                LocalFile localFile2 = LocalFile.Create(image);
                try
                {
                    await azureFileController.UploadFile(FileType.Images, localFile2.LocalPath, localFile2.Stream);
                }
                catch (Exception e)
                {
                    this.Error = "Something bad happened during uploading... Please try again later.";
                    System.Diagnostics.Trace.TraceError("Error during uploading:" + e.ToString());

                    // won't save record, need to clean up AzureFileStorage
                    await azureFileController.DeleteFile(FileType.Images, Path.GetFileName(localFile2.LocalPath));

                    await azureFileController.DeleteFile(FileType.Templates, toAdd.DocumentLink);

                    return(Page());
                }
                toAdd.PreviewImageLink = Path.GetFileName(localFile2.LocalPath);

                this.CurrentUser.Templates.Add(toAdd);
                _context.SaveChangesAsync().Wait();

                return(Redirect("~/templates"));
            }
        }