helpers.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // helpers.js
  2. // This file contains some low level helper functions for the VWF nodeJS server.
  3. var libpath = require( 'path' ),
  4. fs = require( 'fs' );
  5. // List of valid ID characters for use in an instance.
  6. var ValidIDChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  7. // IsInstanceID tests if the passed in potential Instance ID
  8. // is a valid instance id.
  9. function IsInstanceID( potentialInstanceID ) {
  10. if ( potentialInstanceID.match(/^[0-9A-Za-z]{16}$/) ) {
  11. return true;
  12. }
  13. return false;
  14. }
  15. // GenerateInstanceID function creates a randomly generated instance ID.
  16. function GenerateInstanceID( ) {
  17. var text = "";
  18. for( var i=0; i < 16; i++ )
  19. text += ValidIDChars.charAt( Math.floor( Math.random( ) * ValidIDChars.length ) );
  20. return text;
  21. }
  22. // JoinPath
  23. // Takes multiple arguments, joins them together into one path.
  24. function JoinPath( /* arguments */ ) {
  25. var result = "";
  26. if ( arguments.length > 0 ) {
  27. if ( arguments[ 0 ] ) {
  28. result = arguments[ 0 ];
  29. }
  30. for ( var index = 1; index < arguments.length; index++ ) {
  31. var newSegment = arguments[ index ];
  32. if ( newSegment == undefined ) {
  33. newSegment = "";
  34. }
  35. if ( ( newSegment[ 0 ] == "/" ) && ( result[ result.length - 1 ] == "/" ) ) {
  36. result = result + newSegment.slice( 1 );
  37. } else if ( ( newSegment[ 0 ] == "/" ) || ( result[ result.length - 1 ] == "/" ) ) {
  38. result = result + newSegment;
  39. } else {
  40. result = result + "/" + newSegment;
  41. }
  42. //result = libpath.join( result, newSegment );
  43. }
  44. }
  45. return result;
  46. }
  47. // IsDirectory tests if the passed in path exists, and if it is a directory.
  48. function IsDirectory( path ) {
  49. var seperatorFixedPath = path.replace( /\//g, libpath.sep );
  50. if ( ! fs.existsSync( seperatorFixedPath ) ) {
  51. return false;
  52. }
  53. return fs.statSync( seperatorFixedPath ).isDirectory();
  54. }
  55. // IsFile tests if the passed in path exists, and if it is a file.
  56. function IsFile( path ) {
  57. var seperatorFixedPath = path.replace( /\//g, libpath.sep );
  58. if ( ! fs.existsSync( seperatorFixedPath ) ) {
  59. return false;
  60. }
  61. return fs.statSync( seperatorFixedPath ).isFile();
  62. }
  63. // GenerateSegments takes a string, breaks it into
  64. // '/' separated segments, and removes potential
  65. // blank first and last segments.
  66. function GenerateSegments( argument ) {
  67. var result = argument.split("/");
  68. if ( result.length > 0 ) {
  69. if ( result[ 0 ] == "" ) {
  70. result.shift();
  71. }
  72. }
  73. if ( result.length > 0 ) {
  74. if ( result[ result.length - 1 ] == "" ) {
  75. result.pop();
  76. }
  77. }
  78. return result;
  79. }
  80. exports.JoinPath = JoinPath;
  81. exports.IsDirectory = IsDirectory;
  82. exports.IsFile = IsFile;
  83. exports.IsInstanceID = IsInstanceID;
  84. exports.GenerateSegments = GenerateSegments;
  85. exports.GenerateInstanceID = GenerateInstanceID;