Vfs.hx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package;
  2. import haxe.io.Path;
  3. import sys.FileSystem;
  4. import sys.io.File;
  5. using DateTools;
  6. class Vfs {
  7. var physicalPath:String;
  8. public function new(physicalPath:String) {
  9. this.physicalPath = physicalPath;
  10. if (!FileSystem.exists(physicalPath)) {
  11. FileSystem.createDirectory(physicalPath);
  12. }
  13. }
  14. public function overwriteContent(path:String, content:String) {
  15. var path = getPhysicalPath(path).toString();
  16. if (!FileSystem.exists(path)) {
  17. throw 'Cannot overwrite content for $path: file does not exist';
  18. }
  19. File.saveContent(path, content);
  20. }
  21. public function putContent(path:String, content:String) {
  22. var path = getPhysicalPath(path);
  23. FileSystem.createDirectory(path.dir);
  24. File.saveContent(path.toString(), content);
  25. }
  26. public function getContent(path:String):String {
  27. var path = getPhysicalPath(path);
  28. FileSystem.createDirectory(path.dir);
  29. return File.getContent(path.toString());
  30. }
  31. public function close() {
  32. removeDir(physicalPath);
  33. }
  34. public function getPhysicalPath(path:String) {
  35. return new Path(Path.join([physicalPath, path]));
  36. }
  37. static public function removeDir(dir:String):Void {
  38. if (FileSystem.exists(dir)) {
  39. for (item in FileSystem.readDirectory(dir)) {
  40. item = haxe.io.Path.join([dir, item]);
  41. if (FileSystem.isDirectory(item)) {
  42. removeDir(item);
  43. } else {
  44. FileSystem.deleteFile(item);
  45. }
  46. }
  47. FileSystem.deleteDirectory(dir);
  48. }
  49. }
  50. }