PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/ThinkEmailFomatter/Controllers/AttachmentsController.cs

https://bitbucket.org/nicdao/frg-think-emailformatter
C# | 121 lines | 88 code | 11 blank | 22 comment | 6 complexity | f7892e5fa00cf84161c3da7839e89bf3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Configuration;
  7. using System.IO;
  8. using ThinkEmailFormatter.Models.Extensions;
  9. using ThinkEmailFormatter.Models;
  10. namespace ThinkEmailFormatter.Controllers
  11. {
  12. public class AttachmentsController : BaseController
  13. {
  14. public AttachmentsController(IControllerAggregateService controllerAggregateService)
  15. : base(controllerAggregateService)
  16. {
  17. }
  18. public ActionResult Index()
  19. {
  20. string attachmentsRepo = ConfigurationManager.AppSettings["Attachments.Repo"];
  21. // Get all "cshtml" files from that folder
  22. string[] filePaths = Directory.GetFiles(attachmentsRepo, "*");
  23. // For each files, get rid of the extension file
  24. var attachments = filePaths.Select(f => f.Split('\\').Last());
  25. return View(attachments);
  26. }
  27. /// <summary>
  28. /// Check if template exists in the file system.
  29. /// That is the first part of a 2 steps process to Upload
  30. /// a new file to the file system. This first part is asynchronously
  31. /// requested from the client using jQuery. Then, depending on the
  32. /// client response to that first step, the second step(UploadTemplate)
  33. /// may or may not be requested
  34. /// </summary>
  35. /// <param name="fileName"></param>
  36. /// <returns></returns>
  37. [HttpPost]
  38. public JsonResult CheckAttachmentExists(string fileName)
  39. {
  40. try
  41. {
  42. string filePath = string.Format(@"{0}{1}", ConfigurationManager.AppSettings["Attachments.Repo"], fileName);
  43. bool fileExists = System.IO.File.Exists(filePath);
  44. if (fileExists)
  45. {
  46. return Json(new { code = "existingfile", message = "This file already exists." });
  47. }
  48. else
  49. {
  50. return Json(new { code = "newfile", message = "This file does not exist yet." });
  51. }
  52. }
  53. catch (Exception ie)
  54. {
  55. _aggSvc.Log.Exception(ie);
  56. return Json(new { code = "error", message = "An error occured. Please try again." });
  57. }
  58. }
  59. /// <summary>
  60. /// Upload template file to the file system.
  61. /// That is the second part of a 2 steps process to Upload
  62. /// a new file to the file system. The first step was the client asynchronous
  63. /// request(using jQuery) to the "CheckTemplateExists" method
  64. /// </summary>
  65. /// <param name="file"></param>
  66. /// <returns></returns>
  67. [HttpPost]
  68. public ActionResult UploadAttachment(HttpPostedFileBase file)
  69. {
  70. try
  71. {
  72. // Verify that the user selected a file
  73. if (file != null && file.ContentLength > 0)
  74. {
  75. string fileNameWithExt = Path.GetFileName(file.FileName);
  76. string filePath = string.Format(@"{0}{1}", ConfigurationManager.AppSettings["Attachments.Repo"], fileNameWithExt);
  77. bool fileExists = System.IO.File.Exists(filePath);
  78. if (fileExists)
  79. System.IO.File.Delete(filePath);
  80. file.SaveAs(filePath);
  81. }
  82. // redirect back to the index action to show the form once again
  83. return RedirectToAction("Index");
  84. }
  85. catch (Exception ie)
  86. {
  87. _aggSvc.Log.Exception(ie);
  88. return View("Error");
  89. }
  90. }
  91. public ActionResult DeleteAttachment(string fileName)
  92. {
  93. try
  94. {
  95. string filePath = string.Format(@"{0}{1}", ConfigurationManager.AppSettings["Attachments.Repo"], fileName);
  96. bool fileExists = System.IO.File.Exists(filePath);
  97. if (fileExists)
  98. {
  99. System.IO.File.Delete(filePath);
  100. }
  101. return RedirectToAction("Index");
  102. }
  103. catch (Exception ie)
  104. {
  105. _aggSvc.Log.Exception(ie);
  106. return View("Error");
  107. }
  108. }
  109. }
  110. }