BuildOurOwnBabylonJSController.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. var babylonJSPath = Path.Combine(Server.MapPath("~"), rootPath);
  17. var absPath = Path.Combine(babylonJSPath, relPath);
  18. var type = "";
  19. var extension = "";
  20. if (!String.IsNullOrEmpty(relPath))
  21. extension = Path.GetExtension(relPath).ToLower();
  22. switch (extension)
  23. {
  24. case ".js":
  25. case ".babylon":
  26. case ".manifest":
  27. type = "text/javascript";
  28. break;
  29. case ".png":
  30. type = "image/png";
  31. break;
  32. case ".jpeg":
  33. case ".jpg":
  34. type = "image/jpeg";
  35. break;
  36. case ".bmp":
  37. type = "image/bmp";
  38. break;
  39. default:
  40. type = "text/plain";
  41. break;
  42. }
  43. try
  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. var dir = new DirectoryInfo(rootPath);
  56. var subDirs = dir.GetDirectories();
  57. var files = new List<JObject>();
  58. foreach (var directory in subDirs) {
  59. var babylonFiles = directory.GetFiles("*.babylon");
  60. if (babylonFiles.Length == 0) {
  61. continue;
  62. }
  63. foreach (var file in babylonFiles) {
  64. var linkName = directory.Name + (file.Name.Contains(".incremental.babylon") ? " - Incremental" : "");
  65. files.Add(new JObject(
  66. new JProperty("url", Url.Action("Index", "BabylonJSDemo", new { demoFolderName = directory.Name, demoFile = file.Name })),
  67. new JProperty("linkName", linkName)
  68. ));
  69. }
  70. }
  71. var json = new JObject(new JProperty("files", files));
  72. return json.ToString(Newtonsoft.Json.Formatting.None);
  73. }
  74. }
  75. }