BuildOurOwnBabylonJSController.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Web.Mvc;
  2. using System.IO;
  3. using System.Text;
  4. using System;
  5. namespace BuildOurOwnBabylonJSServer.Controllers
  6. {
  7. public class BuildOurOwnBabylonJSController : Controller
  8. {
  9. public const string GetFileContentActionName = "GetFileContent";
  10. [ActionName(BuildOurOwnBabylonJSController.GetFileContentActionName)]
  11. public ActionResult GetFileContent(string rootPath, string relPath)
  12. {
  13. var babylonJSPath = Path.Combine(Server.MapPath("~"), rootPath);
  14. var absPath = Path.Combine(babylonJSPath, relPath);
  15. var type = "";
  16. var extension = "";
  17. if (!String.IsNullOrEmpty(relPath))
  18. extension = Path.GetExtension(relPath).ToLower();
  19. switch (extension)
  20. {
  21. case ".js":
  22. case ".babylon":
  23. case ".manifest":
  24. type = "text/javascript";
  25. break;
  26. case ".png":
  27. type = "image/png";
  28. break;
  29. case ".jpeg":
  30. case ".jpg":
  31. type = "image/jpeg";
  32. break;
  33. case ".bmp":
  34. type = "image/bmp";
  35. break;
  36. default:
  37. type = "text/plain";
  38. break;
  39. }
  40. try
  41. {
  42. return File(new FileStream(absPath, FileMode.Open), type);
  43. }
  44. catch
  45. {
  46. return new HttpNotFoundResult();
  47. }
  48. }
  49. }
  50. }