michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: this.EXPORTED_SYMBOLS = [ michael@0: "DownloadPaths", michael@0: ]; michael@0: michael@0: /** michael@0: * This module provides the DownloadPaths object which contains methods for michael@0: * giving names and paths to files being downloaded. michael@0: * michael@0: * List of methods: michael@0: * michael@0: * nsILocalFile michael@0: * createNiceUniqueFile(nsILocalFile aLocalFile) michael@0: * michael@0: * [string base, string ext] michael@0: * splitBaseNameAndExtension(string aLeafName) michael@0: */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const Cr = Components.results; michael@0: michael@0: this.DownloadPaths = { michael@0: /** michael@0: * Creates a uniquely-named file starting from the name of the provided file. michael@0: * If a file with the provided name already exists, the function attempts to michael@0: * create nice alternatives, like "base(1).ext" (instead of "base-1.ext"). michael@0: * michael@0: * If a unique name cannot be found, the function throws the XPCOM exception michael@0: * NS_ERROR_FILE_TOO_BIG. Other exceptions, like NS_ERROR_FILE_ACCESS_DENIED, michael@0: * can also be expected. michael@0: * michael@0: * @param aTemplateFile michael@0: * nsILocalFile whose leaf name is going to be used as a template. The michael@0: * provided object is not modified. michael@0: * @returns A new instance of an nsILocalFile object pointing to the newly michael@0: * created empty file. On platforms that support permission bits, the michael@0: * file is created with permissions 644. michael@0: */ michael@0: createNiceUniqueFile: function DP_createNiceUniqueFile(aTemplateFile) { michael@0: // Work on a clone of the provided template file object. michael@0: var curFile = aTemplateFile.clone().QueryInterface(Ci.nsILocalFile); michael@0: var [base, ext] = DownloadPaths.splitBaseNameAndExtension(curFile.leafName); michael@0: // Try other file names, for example "base(1).txt" or "base(1).tar.gz", michael@0: // only if the file name initially set already exists. michael@0: for (let i = 1; i < 10000 && curFile.exists(); i++) { michael@0: curFile.leafName = base + "(" + i + ")" + ext; michael@0: } michael@0: // At this point we hand off control to createUnique, which will create the michael@0: // file with the name we chose, if it is valid. If not, createUnique will michael@0: // attempt to modify it again, for example it will shorten very long names michael@0: // that can't be created on some platforms, and for which a normal call to michael@0: // nsIFile.create would result in NS_ERROR_FILE_NOT_FOUND. This can result michael@0: // very rarely in strange names like "base(9999).tar-1.gz" or "ba-1.gz". michael@0: curFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0644); michael@0: return curFile; michael@0: }, michael@0: michael@0: /** michael@0: * Separates the base name from the extension in a file name, recognizing some michael@0: * double extensions like ".tar.gz". michael@0: * michael@0: * @param aLeafName michael@0: * The full leaf name to be parsed. Be careful when processing names michael@0: * containing leading or trailing dots or spaces. michael@0: * @returns [base, ext] michael@0: * The base name of the file, which can be empty, and its extension, michael@0: * which always includes the leading dot unless it's an empty string. michael@0: * Concatenating the two items always results in the original name. michael@0: */ michael@0: splitBaseNameAndExtension: function DP_splitBaseNameAndExtension(aLeafName) { michael@0: // The following regular expression is built from these key parts: michael@0: // .*? Matches the base name non-greedily. michael@0: // \.[A-Z0-9]{1,3} Up to three letters or numbers preceding a michael@0: // double extension. michael@0: // \.(?:gz|bz2|Z) The second part of common double extensions. michael@0: // \.[^.]* Matches any extension or a single trailing dot. michael@0: var [, base, ext] = /(.*?)(\.[A-Z0-9]{1,3}\.(?:gz|bz2|Z)|\.[^.]*)?$/i michael@0: .exec(aLeafName); michael@0: // Return an empty string instead of undefined if no extension is found. michael@0: return [base, ext || ""]; michael@0: } michael@0: };