/toolkit/mozapps/downloads/DownloadPaths.jsm

http://github.com/zpao/v8monkey · Unknown · 121 lines · 116 code · 5 blank · 0 comment · 0 complexity · 7376240e0b907d7785bc77bdef2801ed MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
  3. /* ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is Download Manager Utility Code.
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Paolo Amadini <http://www.amadzone.org/>.
  20. * Portions created by the Initial Developer are Copyright (C) 2010
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either the GNU General Public License Version 2 or later (the "GPL"), or
  27. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. var EXPORTED_SYMBOLS = [
  39. "DownloadPaths",
  40. ];
  41. /**
  42. * This module provides the DownloadPaths object which contains methods for
  43. * giving names and paths to files being downloaded.
  44. *
  45. * List of methods:
  46. *
  47. * nsILocalFile
  48. * createNiceUniqueFile(nsILocalFile aLocalFile)
  49. *
  50. * [string base, string ext]
  51. * splitBaseNameAndExtension(string aLeafName)
  52. */
  53. const Cc = Components.classes;
  54. const Ci = Components.interfaces;
  55. const Cu = Components.utils;
  56. const Cr = Components.results;
  57. const DownloadPaths = {
  58. /**
  59. * Creates a uniquely-named file starting from the name of the provided file.
  60. * If a file with the provided name already exists, the function attempts to
  61. * create nice alternatives, like "base(1).ext" (instead of "base-1.ext").
  62. *
  63. * If a unique name cannot be found, the function throws the XPCOM exception
  64. * NS_ERROR_FILE_TOO_BIG. Other exceptions, like NS_ERROR_FILE_ACCESS_DENIED,
  65. * can also be expected.
  66. *
  67. * @param aTemplateFile
  68. * nsILocalFile whose leaf name is going to be used as a template. The
  69. * provided object is not modified.
  70. * @returns A new instance of an nsILocalFile object pointing to the newly
  71. * created empty file. On platforms that support permission bits, the
  72. * file is created with permissions 600.
  73. */
  74. createNiceUniqueFile: function DP_createNiceUniqueFile(aTemplateFile) {
  75. // Work on a clone of the provided template file object.
  76. var curFile = aTemplateFile.clone().QueryInterface(Ci.nsILocalFile);
  77. var [base, ext] = DownloadPaths.splitBaseNameAndExtension(curFile.leafName);
  78. // Try other file names, for example "base(1).txt" or "base(1).tar.gz",
  79. // only if the file name initially set already exists.
  80. for (let i = 1; i < 10000 && curFile.exists(); i++) {
  81. curFile.leafName = base + "(" + i + ")" + ext;
  82. }
  83. // At this point we hand off control to createUnique, which will create the
  84. // file with the name we chose, if it is valid. If not, createUnique will
  85. // attempt to modify it again, for example it will shorten very long names
  86. // that can't be created on some platforms, and for which a normal call to
  87. // nsIFile.create would result in NS_ERROR_FILE_NOT_FOUND. This can result
  88. // very rarely in strange names like "base(9999).tar-1.gz" or "ba-1.gz".
  89. curFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
  90. return curFile;
  91. },
  92. /**
  93. * Separates the base name from the extension in a file name, recognizing some
  94. * double extensions like ".tar.gz".
  95. *
  96. * @param aLeafName
  97. * The full leaf name to be parsed. Be careful when processing names
  98. * containing leading or trailing dots or spaces.
  99. * @returns [base, ext]
  100. * The base name of the file, which can be empty, and its extension,
  101. * which always includes the leading dot unless it's an empty string.
  102. * Concatenating the two items always results in the original name.
  103. */
  104. splitBaseNameAndExtension: function DP_splitBaseNameAndExtension(aLeafName) {
  105. // The following regular expression is built from these key parts:
  106. // .*? Matches the base name non-greedily.
  107. // \.[A-Z0-9]{1,3} Up to three letters or numbers preceding a
  108. // double extension.
  109. // \.(?:gz|bz2|Z) The second part of common double extensions.
  110. // \.[^.]* Matches any extension or a single trailing dot.
  111. var [, base, ext] = /(.*?)(\.[A-Z0-9]{1,3}\.(?:gz|bz2|Z)|\.[^.]*)?$/i
  112. .exec(aLeafName);
  113. // Return an empty string instead of undefined if no extension is found.
  114. return [base, ext || ""];
  115. }
  116. };