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.

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

mercurial