/ThinkEmailFomatter/Controllers/AttachmentsController.cs
C# | 121 lines | 88 code | 11 blank | 22 comment | 6 complexity | f7892e5fa00cf84161c3da7839e89bf3 MD5 | raw file
Possible License(s): BSD-3-Clause
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Configuration;
- using System.IO;
- using ThinkEmailFormatter.Models.Extensions;
- using ThinkEmailFormatter.Models;
- namespace ThinkEmailFormatter.Controllers
- {
- public class AttachmentsController : BaseController
- {
- public AttachmentsController(IControllerAggregateService controllerAggregateService)
- : base(controllerAggregateService)
- {
- }
- public ActionResult Index()
- {
- string attachmentsRepo = ConfigurationManager.AppSettings["Attachments.Repo"];
- // Get all "cshtml" files from that folder
- string[] filePaths = Directory.GetFiles(attachmentsRepo, "*");
- // For each files, get rid of the extension file
- var attachments = filePaths.Select(f => f.Split('\\').Last());
- return View(attachments);
- }
- /// <summary>
- /// Check if template exists in the file system.
- /// That is the first part of a 2 steps process to Upload
- /// a new file to the file system. This first part is asynchronously
- /// requested from the client using jQuery. Then, depending on the
- /// client response to that first step, the second step(UploadTemplate)
- /// may or may not be requested
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- [HttpPost]
- public JsonResult CheckAttachmentExists(string fileName)
- {
- try
- {
- string filePath = string.Format(@"{0}{1}", ConfigurationManager.AppSettings["Attachments.Repo"], fileName);
- bool fileExists = System.IO.File.Exists(filePath);
- if (fileExists)
- {
- return Json(new { code = "existingfile", message = "This file already exists." });
- }
- else
- {
- return Json(new { code = "newfile", message = "This file does not exist yet." });
- }
- }
- catch (Exception ie)
- {
- _aggSvc.Log.Exception(ie);
- return Json(new { code = "error", message = "An error occured. Please try again." });
- }
- }
- /// <summary>
- /// Upload template file to the file system.
- /// That is the second part of a 2 steps process to Upload
- /// a new file to the file system. The first step was the client asynchronous
- /// request(using jQuery) to the "CheckTemplateExists" method
- /// </summary>
- /// <param name="file"></param>
- /// <returns></returns>
- [HttpPost]
- public ActionResult UploadAttachment(HttpPostedFileBase file)
- {
- try
- {
- // Verify that the user selected a file
- if (file != null && file.ContentLength > 0)
- {
- string fileNameWithExt = Path.GetFileName(file.FileName);
- string filePath = string.Format(@"{0}{1}", ConfigurationManager.AppSettings["Attachments.Repo"], fileNameWithExt);
- bool fileExists = System.IO.File.Exists(filePath);
- if (fileExists)
- System.IO.File.Delete(filePath);
- file.SaveAs(filePath);
- }
- // redirect back to the index action to show the form once again
- return RedirectToAction("Index");
- }
- catch (Exception ie)
- {
- _aggSvc.Log.Exception(ie);
- return View("Error");
- }
- }
- public ActionResult DeleteAttachment(string fileName)
- {
- try
- {
- string filePath = string.Format(@"{0}{1}", ConfigurationManager.AppSettings["Attachments.Repo"], fileName);
- bool fileExists = System.IO.File.Exists(filePath);
- if (fileExists)
- {
- System.IO.File.Delete(filePath);
- }
- return RedirectToAction("Index");
- }
- catch (Exception ie)
- {
- _aggSvc.Log.Exception(ie);
- return View("Error");
- }
- }
- }
- }