BuildOurOwnBabylonJSController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. case ".css":
  42. type = "text/css";
  43. break;
  44. default:
  45. type = "text/plain";
  46. break;
  47. }
  48. return File(new FileStream(absPath, FileMode.Open), type);
  49. }
  50. catch
  51. {
  52. return new HttpNotFoundResult();
  53. }
  54. }
  55. [ActionName(BuildOurOwnBabylonJSController.GetBabylonScenesActionName)]
  56. public string GetBabylonScenes(string rootPath)
  57. {
  58. try
  59. {
  60. var dir = new DirectoryInfo(rootPath);
  61. var subDirs = dir.GetDirectories();
  62. var files = new List<JObject>();
  63. foreach (var directory in subDirs)
  64. {
  65. var babylonFiles = directory.GetFiles("*.babylon");
  66. if (babylonFiles.Length == 0)
  67. continue;
  68. foreach (var file in babylonFiles)
  69. {
  70. var linkName = directory.Name + "/" + Path.GetFileNameWithoutExtension(file.Name);
  71. files.Add(new JObject(
  72. new JProperty("url", Url.Action("Index", "BabylonJSDemo", new { demoFolderName = directory.Name, demoFile = file.Name })),
  73. new JProperty("linkName", linkName)
  74. ));
  75. }
  76. }
  77. var json = new JObject(new JProperty("files", files));
  78. return json.ToString(Newtonsoft.Json.Formatting.None);
  79. }
  80. catch
  81. {
  82. var json = new JObject(new JProperty("files", ""));
  83. return json.ToString(Newtonsoft.Json.Formatting.None);
  84. }
  85. }
  86. }
  87. }