BuildOurOwnBabylonJSController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Web.Mvc;
  2. using System.IO;
  3. using System.Text;
  4. using System;
  5. using Newtonsoft.Json.Linq;
  6. using System.Collections.Generic;
  7. namespace BuildOurOwnBabylonJSServer.Controllers
  8. {
  9. public class BuildOurOwnBabylonJSController : Controller
  10. {
  11. public const string GetFileContentActionName = "GetFileContent";
  12. public const string GetBabylonScenesActionName = "GetBabylonScenes";
  13. [ActionName(BuildOurOwnBabylonJSController.GetFileContentActionName)]
  14. public ActionResult GetFileContent(string rootPath, string relPath)
  15. {
  16. try
  17. {
  18. var babylonJSPath = Path.Combine(Server.MapPath("~"), rootPath);
  19. var absPath = Path.Combine(babylonJSPath, relPath);
  20. var type = "";
  21. var extension = "";
  22. if (!String.IsNullOrEmpty(relPath))
  23. extension = Path.GetExtension(relPath).ToLower();
  24. switch (extension)
  25. {
  26. case ".js":
  27. case ".babylon":
  28. case ".manifest":
  29. type = "text/javascript";
  30. break;
  31. case ".png":
  32. type = "image/png";
  33. break;
  34. case ".jpeg":
  35. case ".jpg":
  36. type = "image/jpeg";
  37. break;
  38. case ".bmp":
  39. type = "image/bmp";
  40. break;
  41. default:
  42. type = "text/plain";
  43. break;
  44. }
  45. return File(new FileStream(absPath, FileMode.Open), type);
  46. }
  47. catch
  48. {
  49. return new HttpNotFoundResult();
  50. }
  51. }
  52. [ActionName(BuildOurOwnBabylonJSController.GetBabylonScenesActionName)]
  53. public string GetBabylonScenes(string rootPath)
  54. {
  55. try
  56. {
  57. var dir = new DirectoryInfo(rootPath);
  58. var subDirs = dir.GetDirectories();
  59. var files = new List<JObject>();
  60. foreach (var directory in subDirs)
  61. {
  62. var babylonFiles = directory.GetFiles("*.babylon");
  63. if (babylonFiles.Length == 0)
  64. continue;
  65. foreach (var file in babylonFiles)
  66. {
  67. var linkName = directory.Name + (file.Name.Contains(".incremental.babylon") ? " - Incremental" : "");
  68. files.Add(new JObject(
  69. new JProperty("url", Url.Action("Index", "BabylonJSDemo", new { demoFolderName = directory.Name, demoFile = file.Name })),
  70. new JProperty("linkName", linkName)
  71. ));
  72. }
  73. }
  74. var json = new JObject(new JProperty("files", files));
  75. return json.ToString(Newtonsoft.Json.Formatting.None);
  76. }
  77. catch
  78. {
  79. var json = new JObject(new JProperty("files", ""));
  80. return json.ToString(Newtonsoft.Json.Formatting.None);
  81. }
  82. }
  83. }
  84. }