storagefs.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // storagefs.js
  2. // This file contains the functions that handle filesystem based storage.
  3. var helpers = require( './helpers' ),
  4. fs = require( 'fs' ),
  5. url = require( 'url' ),
  6. libpath = require( 'path' );
  7. var storage_root = "./documents";
  8. function list_application_instances( public_path, application ) {
  9. var result = [ ];
  10. var potential_app_dir = helpers.JoinPath( storage_root, public_path, application );
  11. if ( helpers.IsDirectory( potential_app_dir ) ) {
  12. var potential_instances = fs.readdirSync( potential_app_dir.replace( /\//g, libpath.sep ) );
  13. for ( var index = 0; index < potential_instances; index++ ) {
  14. if ( ( helpers.IsDirectory( helpers.JoinPath( potential_app_dir, potential_instances[ index ] ) ) ) && ( potential_instances[ index ].match( /^instance_*/ ) ) ) {
  15. var potential_instance_id = potential_instances[ index ].slice( 9 );
  16. if ( ( get_instance_metadata( public_path, application, potential_instance_id ) != undefined ) && ( get_persistence_state( public_path, application, potential_instance_id ) != undefined ) && ( list_instance_save_states( public_path, application, potential_instance_id ).length == 0 ) ) {
  17. result.push( potential_instance_id );
  18. }
  19. }
  20. }
  21. }
  22. return result.sort();
  23. }
  24. function get_instance_metadata( public_path, application, instance ) {
  25. result = undefined;
  26. potential_filename = helpers.JoinPath( storage_root, public_path, application, "instance_" + instance, "metadata.json" );
  27. if ( helpers.IsFile( potential_filename ) ) {
  28. var file_contents = fs.readFileSync( potential_filename, "utf8" );
  29. result = JSON.parse( file_contents );
  30. }
  31. return result;
  32. }
  33. function set_instance_metadata( public_path, application, instance, metadata ) {
  34. var segments = helpers.generateSegments( public_path );
  35. segments.push( application );
  36. segments.push( "instance_" + instance );
  37. var current_path = storage_root;
  38. for ( var index = 0; index < segments.length; index++ ) {
  39. current_path = helpers.JoinPath( current_path, segments[ index ] );
  40. if ( !( helpers.IsDirectory( current_path ) ) ) {
  41. fs.mkdirSync( current_path );
  42. }
  43. }
  44. fs.writeFileSync( helpers.JoinPath( storage_root, public_path, application, "instance_" + instance, "metadata.json" ), JSON.stringify( metadata ) );
  45. }
  46. function get_persistence_state( public_path, application, instance ) {
  47. result = undefined;
  48. potential_filename = helpers.JoinPath( storage_root, public_path, application, "instance_" + instance, "persistenceState.vwf.json" );
  49. if ( helpers.IsFile( potential_filename ) ) {
  50. var file_contents = fs.readFileSync( potential_filename, "utf8" );
  51. result = JSON.parse( file_contents );
  52. }
  53. return result;
  54. }
  55. // WARNING: Take note, if an instance already has metadata, but you provide undefined metadata, the
  56. // old metadata will persist.
  57. function set_persistence_state( public_path, application, instance, state, metadata ) {
  58. var segments = helpers.generateSegments( public_path );
  59. segments.push( application );
  60. segments.push( "instance_" + instance );
  61. var current_path = storage_root;
  62. for ( var index = 0; index < segments.length; index++ ) {
  63. current_path = helpers.JoinPath( current_path, segments[ index ] );
  64. if ( !( helpers.IsDirectory( current_path ) ) ) {
  65. fs.mkdirSync( current_path );
  66. }
  67. }
  68. fs.writeFileSync( helpers.JoinPath( storage_root, public_path, application, "instance_" + instance, "persistenceState.vwf.json" ), JSON.stringify( state ) );
  69. if ( metadata ) {
  70. fs.writeFileSync( helpers.JoinPath( storage_root, public_path, application, "instance_" + instance, "metadata.json" ), JSON.stringify( metadata ) );
  71. }
  72. }
  73. function list_instance_save_states( public_path, application, instance ) {
  74. var result = [ ];
  75. var potential_dir_name = helpers.JoinPath( storage_root, public_path, application, "instance_" + instance );
  76. if ( helpers.IsDirectory( potential_dir_name ) ) {
  77. var potential_save_names = fs.readdirSync( potential_dir_name.replace( /\//g, libpath.sep ) );
  78. for ( var index = 0; index < potential_save_names.length; index++ ) {
  79. if ( ( potential_save_names[ index ].match( /^save_*/ ) ) && ( helpers.IsDirectory( helpers.JoinPath( potential_dir_name, potential_save_names[ index ] ) ) ) ) {
  80. if ( helpers.IsFile( helpers.JoinPath( potential_dir_name, potential_save_names[ index ], "saveState.vwf.json" ) ) ) {
  81. result.push( potential_save_names[ index ].slice( 5 ) );
  82. }
  83. }
  84. }
  85. }
  86. return result.sort();
  87. }
  88. function list_application_save_states( public_path, application ) {
  89. result = {};
  90. var application_dir_name = helpers.JoinPath( storage_root, public_path, application );
  91. if ( helpers.IsDirectory( application_dir_name ) ) {
  92. var potential_instances = fs.readdirSync( application_dir_name.replace( /\//g, libpath.sep ) );
  93. for ( var index = 0; index < potential_instances.length; index++ ) {
  94. if ( potential_instances[ index ].match( /^instance_*/ ) ) {
  95. var instance_id = potential_instances[ index ].slice( 9 );
  96. var instance_saves = list_instance_save_states( public_path, application, instance_id );
  97. if ( instance_saves.length > 0 ) {
  98. result[ instance_id ] = instance_saves;
  99. }
  100. }
  101. }
  102. }
  103. return result;
  104. }
  105. function get_save_metadata( public_path, application, instance, save_name ) {
  106. result = undefined;
  107. potential_filename = helpers.JoinPath( storage_root, public_path, application, "instance_" + instance, "save_" + save_name, "metadata.json" );
  108. if ( helpers.IsFile( potential_filename ) ) {
  109. var file_contents = fs.readFileSync( potential_filename, "utf8" );
  110. result = JSON.parse( file_contents );
  111. } else {
  112. potential_filename = helpers.JoinPath( storage_root, public_path, application, "instance_" + instance, "metadata.json" );
  113. if ( helpers.IsFile( potential_filename ) ) {
  114. var file_contents = fs.readFileSync( potential_filename, "utf8" );
  115. result = JSON.parse( file_contents );
  116. }
  117. }
  118. return result;
  119. }
  120. function get_save_state( public_path, application, instance, save_name ) {
  121. result = undefined;
  122. potential_filename = helpers.JoinPath( storage_root, public_path, application, "instance_" + instance, "save_" + save_name, "saveState.vwf.json" );
  123. if ( helpers.IsFile( potential_filename ) ) {
  124. var file_contents = fs.readFileSync( potential_filename, "utf8" );
  125. result = JSON.parse( file_contents );
  126. }
  127. return result;
  128. }
  129. function set_save_state( public_path, application, instance, save_name, save_state, metadata ) {
  130. var segments = helpers.generateSegments( public_path );
  131. segments.push( application );
  132. segments.push( "instance_" + instance );
  133. segments.push( "save_" + save_name );
  134. var current_path = storage_root;
  135. for ( var index = 0; index < segments.length; index++ ) {
  136. current_path = helpers.JoinPath( current_path, segments[ index ] );
  137. if ( !( helpers.IsDirectory( current_path ) ) ) {
  138. fs.mkdirSync( current_path );
  139. }
  140. }
  141. fs.writeFileSync( helpers.JoinPath( storage_root, public_path, application, "instance_" + instance, "save_" + save_name, "saveState.vwf.json" ), JSON.stringify( save_state ) );
  142. if ( metadata ) {
  143. fs.writeFileSync( helpers.JoinPath( storage_root, public_path, application, "instance_" + instance, "save_" + save_name, "metadata.json" ), JSON.stringify( metadata ) );
  144. }
  145. }
  146. exports.ListApplicationInstances = list_application_instances;
  147. exports.GetInstanceMetadata = get_instance_metadata;
  148. exports.SetInstanceMetadata = set_instance_metadata;
  149. exports.GetPersistenceState = get_persistence_state;
  150. exports.SetPersistenceState = set_persistence_state;
  151. exports.ListInstanceSaveStates = list_instance_save_states;
  152. exports.ListApplicationSaveStates = list_application_save_states;
  153. exports.GetSaveMetadata = get_save_metadata;
  154. exports.GetSaveState = get_save_state;
  155. exports.SetSaveState = set_save_state;