toolkit/mozapps/downloads/DownloadPaths.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 this.EXPORTED_SYMBOLS = [
     8   "DownloadPaths",
     9 ];
    11 /**
    12  * This module provides the DownloadPaths object which contains methods for
    13  * giving names and paths to files being downloaded.
    14  *
    15  * List of methods:
    16  *
    17  * nsILocalFile
    18  * createNiceUniqueFile(nsILocalFile aLocalFile)
    19  *
    20  * [string base, string ext]
    21  * splitBaseNameAndExtension(string aLeafName)
    22  */
    24 const Cc = Components.classes;
    25 const Ci = Components.interfaces;
    26 const Cu = Components.utils;
    27 const Cr = Components.results;
    29 this.DownloadPaths = {
    30   /**
    31    * Creates a uniquely-named file starting from the name of the provided file.
    32    * If a file with the provided name already exists, the function attempts to
    33    * create nice alternatives, like "base(1).ext" (instead of "base-1.ext").
    34    *
    35    * If a unique name cannot be found, the function throws the XPCOM exception
    36    * NS_ERROR_FILE_TOO_BIG. Other exceptions, like NS_ERROR_FILE_ACCESS_DENIED,
    37    * can also be expected.
    38    *
    39    * @param aTemplateFile
    40    *        nsILocalFile whose leaf name is going to be used as a template. The
    41    *        provided object is not modified.
    42    * @returns A new instance of an nsILocalFile object pointing to the newly
    43    *          created empty file. On platforms that support permission bits, the
    44    *          file is created with permissions 644.
    45    */
    46   createNiceUniqueFile: function DP_createNiceUniqueFile(aTemplateFile) {
    47     // Work on a clone of the provided template file object.
    48     var curFile = aTemplateFile.clone().QueryInterface(Ci.nsILocalFile);
    49     var [base, ext] = DownloadPaths.splitBaseNameAndExtension(curFile.leafName);
    50     // Try other file names, for example "base(1).txt" or "base(1).tar.gz",
    51     // only if the file name initially set already exists.
    52     for (let i = 1; i < 10000 && curFile.exists(); i++) {
    53       curFile.leafName = base + "(" + i + ")" + ext;
    54     }
    55     // At this point we hand off control to createUnique, which will create the
    56     // file with the name we chose, if it is valid. If not, createUnique will
    57     // attempt to modify it again, for example it will shorten very long names
    58     // that can't be created on some platforms, and for which a normal call to
    59     // nsIFile.create would result in NS_ERROR_FILE_NOT_FOUND. This can result
    60     // very rarely in strange names like "base(9999).tar-1.gz" or "ba-1.gz".
    61     curFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0644);
    62     return curFile;
    63   },
    65   /**
    66    * Separates the base name from the extension in a file name, recognizing some
    67    *  double extensions like ".tar.gz".
    68    *
    69    * @param aLeafName
    70    *        The full leaf name to be parsed. Be careful when processing names
    71    *        containing leading or trailing dots or spaces.
    72    * @returns [base, ext]
    73    *          The base name of the file, which can be empty, and its extension,
    74    *          which always includes the leading dot unless it's an empty string.
    75    *          Concatenating the two items always results in the original name.
    76    */
    77   splitBaseNameAndExtension: function DP_splitBaseNameAndExtension(aLeafName) {
    78     // The following regular expression is built from these key parts:
    79     //  .*?                      Matches the base name non-greedily.
    80     //  \.[A-Z0-9]{1,3}          Up to three letters or numbers preceding a
    81     //                           double extension.
    82     //  \.(?:gz|bz2|Z)           The second part of common double extensions.
    83     //  \.[^.]*                  Matches any extension or a single trailing dot.
    84     var [, base, ext] = /(.*?)(\.[A-Z0-9]{1,3}\.(?:gz|bz2|Z)|\.[^.]*)?$/i
    85                         .exec(aLeafName);
    86     // Return an empty string instead of undefined if no extension is found.
    87     return [base, ext || ""];
    88   }
    89 };

mercurial