/_workbench/filesystem/file_put_contents.js

http://github.com/kvz/phpjs · JavaScript · 104 lines · 56 code · 14 blank · 34 comment · 11 complexity · 57e1b05cc825018a6004872018f2b868 MD5 · raw file

  1. function file_put_contents (aFile, data, flags, context) {
  2. // http://kevin.vanzonneveld.net
  3. // + original by: Brett Zamir (http://brett-zamir.me)
  4. // % note 1: Only works at present in Mozilla (and unfinished too); might also allow context to determine
  5. // % note 1: whether for Mozilla, for HTTP PUT or POST requests, etc.
  6. // * example 1: file_put_contents('file://Users/Kevin/someFile.txt', 'hello');
  7. // * returns 1: 5
  8. // Fix: allow file to be placed outside of profile directory; fix: add PHP-style flags, etc.
  9. var opts = 0, i = 0;
  10. // Initialize binary arguments. Both the string & integer (constant) input is
  11. // allowed
  12. var OPTS = {
  13. FILE_USE_INCLUDE_PATH : 1,
  14. LOCK_EX : 2,
  15. FILE_APPEND : 8,
  16. FILE_TEXT : 32,
  17. FILE_BINARY : 64
  18. };
  19. if (typeof flags === 'number') {
  20. opts = flags;
  21. }
  22. else { // Allow for a single string or an array of string flags
  23. flags = [].concat(flags);
  24. for (i=0; i < flags.length; i++) {
  25. if (OPTS[flags[i]]) {
  26. opts = opts | OPTS[flags[i]];
  27. } else {
  28. }
  29. }
  30. }
  31. var append = opts & OPTS.FILE_APPEND;
  32. var charset = 'UTF-8'; // Can be any character encoding name that Mozilla supports
  33. // Setting earlier, but even with a different setting, it still seems to save as UTF-8
  34. // var em = Cc['@mozilla.org/extensions/manager;1'].
  35. // getService(Ci.nsIExtensionManager);
  36. // the path may use forward slash ('/') as the delimiter
  37. // var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, 'content/'+filename);
  38. var file;
  39. if ((/^file:\/\//).test(aFile)) {
  40. // absolute file URL
  41. var ios = Components.classes["@mozilla.org/network/io-service;1"].
  42. getService(Components.interfaces.nsIIOService);
  43. var URL = ios.newURI(aFile, null, null);
  44. file = URL.QueryInterface(Components.interfaces.nsIFileURL).file; // becomes nsIFile
  45. }
  46. else {
  47. //native
  48. file = Components.classes["@mozilla.org/file/local;1"].
  49. createInstance(Components.interfaces.nsILocalFile);
  50. file.initWithPath(aFile); // e.g., "/home"
  51. }
  52. /**
  53. //tmp
  54. var file = Components.classes["@mozilla.org/file/directory_service;1"].
  55. getService(Components.interfaces.nsIProperties).
  56. get("TmpD", Components.interfaces.nsIFile);
  57. file.append("suggestedName.tmp");
  58. file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 438); // 0666
  59. //
  60. var file = Components.classes["@mozilla.org/file/directory_service;1"].
  61. getService(Components.interfaces.nsIProperties).
  62. get("ProfD", Components.interfaces.nsIFile);
  63. //*/
  64. if (typeof file === 'string') {
  65. var tempfilename = file;
  66. file = Cc['@mozilla.org/file/directory_service;1'].getService(Ci.nsIProperties).get('ProfD', Ci.nsILocalFile);
  67. file.append(tempfilename);
  68. }
  69. if( !file.exists() ) { // if it doesn't exist, create // || !file.isDirectory()
  70. file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 511); // DIRECTORY_TYPE (0777)
  71. }
  72. else if (!append) {
  73. file.remove(false); // I needed to do this to avoid some apparent race condition (the effect of junk getting added to the end)
  74. file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 511); // DIRECTORY_TYPE (0777)
  75. }
  76. // file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 436); // for temporary (0664)
  77. var foStream = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
  78. var fileFlags = append ? (0x02 | 0x10) : 0x02; // use 0x02 | 0x10 to open file for appending.
  79. foStream.init(file, fileFlags, 436, 0); // 436 is 0664
  80. // foStream.init(file, 0x02 | 0x08 | 0x20, 436, 0); // write, create, truncate (436 is 0664)
  81. var os = Cc['@mozilla.org/intl/converter-output-stream;1'].createInstance(Ci.nsIConverterOutputStream);
  82. // This assumes that foStream is the nsIOutputStream you want to write to
  83. // 0x0000 instead of '?' will produce an exception: http://www.xulplanet.com/references/xpcomref/ifaces/nsIConverterOutputStream.html
  84. // If charset in xsl:output is ISO-8859-1, the file won't open--if it is GB2312, it will output as UTF-8--seems buggy or?
  85. os.init(foStream, charset, 0, 0x0000);
  86. os.writeString(data);
  87. os.close();
  88. foStream.close();
  89. // todo: return number of bytes written or false on failure
  90. }