Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_BASE_FILENAME_UTIL_H_ |
| 6 | #define NET_BASE_FILENAME_UTIL_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
dhnishi | c28dfc3 | 2015-07-08 02:21:31 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 11 | #include "net/base/net_export.h" |
| 12 | |
| 13 | class GURL; |
| 14 | |
| 15 | namespace base { |
| 16 | class FilePath; |
| 17 | } |
| 18 | |
Xing Liu | 2a83222 | 2021-11-17 01:43:09 | [diff] [blame] | 19 | namespace net { |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 20 | |
| 21 | // Given the full path to a file name, creates a file: URL. The returned URL |
| 22 | // may not be valid if the input is malformed. |
| 23 | NET_EXPORT GURL FilePathToFileURL(const base::FilePath& path); |
| 24 | |
| 25 | // Converts a file: URL back to a filename that can be passed to the OS. The |
| 26 | // file URL must be well-formed (GURL::is_valid() must return true); we don't |
Chris Mumford | e8d8f33 | 2018-10-10 02:23:31 | [diff] [blame] | 27 | // handle degenerate cases here. Returns true on success, false if |url| is |
| 28 | // invalid or the file path cannot be extracted from |url|. |
| 29 | // On failure, *file_path will be empty. |
| 30 | // |
Tommy Li | 3187fae | 2019-11-14 20:04:22 | [diff] [blame] | 31 | // Do not call this with a |url| that doesn't have a file:// scheme. |
| 32 | // The implementation is specific to the platform filesystem, and not |
| 33 | // applicable to other schemes. |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 34 | NET_EXPORT bool FileURLToFilePath(const GURL& url, base::FilePath* file_path); |
| 35 | |
| 36 | // Generates a filename using the first successful method from the following (in |
| 37 | // order): |
| 38 | // |
| 39 | // 1) The raw Content-Disposition header in |content_disposition| as read from |
| 40 | // the network. |referrer_charset| is used to decode non-ASCII strings. |
| 41 | // 2) |suggested_name| if specified. |suggested_name| is assumed to be in |
| 42 | // UTF-8. |
| 43 | // 3) The filename extracted from the |url|. |referrer_charset| will be used to |
Xing Liu | 3229908 | 2019-01-30 03:55:29 | [diff] [blame] | 44 | // interpret the URL if there are non-ascii characters.The file extension for |
| 45 | // filenames extracted from the URL are considered unreliable if the URL |
| 46 | // contains a query string. If a MIME type is available (i.e. |mime_type| is |
| 47 | // not empty) and that MIME type has a preferred extension, then the |
| 48 | // resulting filename will have that preferred extension. |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 49 | // 4) |default_name|. If non-empty, |default_name| is assumed to be a filename |
| 50 | // and shouldn't contain a path. |default_name| is not subject to validation |
| 51 | // or sanitization, and therefore shouldn't be a user supplied string. |
| 52 | // 5) The hostname portion from the |url| |
| 53 | // |
| 54 | // Then, leading and trailing '.'s will be removed. On Windows, trailing spaces |
| 55 | // are also removed. The string "download" is the final fallback if no filename |
| 56 | // is found or the filename is empty. |
| 57 | // |
| 58 | // Any illegal characters in the filename will be replaced by '-'. If the |
| 59 | // filename doesn't contain an extension, and a |mime_type| is specified, the |
| 60 | // preferred extension for the |mime_type| will be appended to the filename. |
| 61 | // The resulting filename is then checked against a list of reserved names on |
| 62 | // Windows. If the name is reserved, an underscore will be prepended to the |
| 63 | // filename. |
| 64 | // |
| 65 | // Note: |mime_type| should only be specified if this function is called from a |
| 66 | // thread that allows IO. |
Jan Wilken Dörrie | 739ccc21 | 2021-03-11 18:13:05 | [diff] [blame] | 67 | NET_EXPORT std::u16string GetSuggestedFilename( |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 68 | const GURL& url, |
| 69 | const std::string& content_disposition, |
| 70 | const std::string& referrer_charset, |
| 71 | const std::string& suggested_name, |
| 72 | const std::string& mime_type, |
| 73 | const std::string& default_name); |
| 74 | |
| 75 | // Similar to GetSuggestedFilename(), but returns a FilePath. |
| 76 | NET_EXPORT base::FilePath GenerateFileName( |
| 77 | const GURL& url, |
| 78 | const std::string& content_disposition, |
| 79 | const std::string& referrer_charset, |
| 80 | const std::string& suggested_name, |
| 81 | const std::string& mime_type, |
| 82 | const std::string& default_name); |
| 83 | |
Xing Liu | 3229908 | 2019-01-30 03:55:29 | [diff] [blame] | 84 | // Similar to GetSuggestedFilename(). If |should_replace_extension| is true, the |
| 85 | // file extension extracted from a URL will always be considered unreliable and |
| 86 | // the file extension will be determined by |mime_type|. |
| 87 | NET_EXPORT base::FilePath GenerateFileName( |
| 88 | const GURL& url, |
| 89 | const std::string& content_disposition, |
| 90 | const std::string& referrer_charset, |
| 91 | const std::string& suggested_name, |
| 92 | const std::string& mime_type, |
| 93 | const std::string& default_name, |
| 94 | bool should_replace_extension); |
| 95 | |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 96 | // Valid components: |
| 97 | // * are not empty |
| 98 | // * are not Windows reserved names (CON, NUL.zip, etc.) |
| 99 | // * do not have trailing separators |
| 100 | // * do not equal kCurrentDirectory |
| 101 | // * do not reference the parent directory |
| 102 | // * do not contain illegal characters |
| 103 | // * do not end with Windows shell-integrated extensions (even on posix) |
| 104 | // * do not begin with '.' (which would hide them in most file managers) |
| 105 | // * do not end with ' ' or '.' |
| 106 | NET_EXPORT bool IsSafePortablePathComponent(const base::FilePath& component); |
| 107 | |
| 108 | // Basenames of valid relative paths are IsSafePortableBasename(), and internal |
| 109 | // path components of valid relative paths are valid path components as |
| 110 | // described above IsSafePortableBasename(). Valid relative paths are not |
| 111 | // absolute paths. |
| 112 | NET_EXPORT bool IsSafePortableRelativePath(const base::FilePath& path); |
| 113 | |
| 114 | // Ensures that the filename and extension is safe to use in the filesystem. |
| 115 | // |
| 116 | // Assumes that |file_path| already contains a valid path or file name. On |
| 117 | // Windows if the extension causes the file to have an unsafe interaction with |
| 118 | // the shell (see net_util::IsShellIntegratedExtension()), then it will be |
| 119 | // replaced by the string 'download'. If |file_path| doesn't contain an |
| 120 | // extension or |ignore_extension| is true then the preferred extension, if one |
| 121 | // exists, for |mime_type| will be used as the extension. |
| 122 | // |
| 123 | // On Windows, the filename will be checked against a set of reserved names, and |
| 124 | // if so, an underscore will be prepended to the name. |
| 125 | // |
| 126 | // |file_name| can either be just the file name or it can be a full path to a |
| 127 | // file. |
| 128 | // |
| 129 | // Note: |mime_type| should only be non-empty if this function is called from a |
| 130 | // thread that allows IO. |
| 131 | NET_EXPORT void GenerateSafeFileName(const std::string& mime_type, |
| 132 | bool ignore_extension, |
| 133 | base::FilePath* file_path); |
| 134 | |
dhnishi | c28dfc3 | 2015-07-08 02:21:31 | [diff] [blame] | 135 | // Returns whether the specified file name is a reserved name on Windows. |
| 136 | // This includes names like "com2.zip" (which correspond to devices) and |
| 137 | // desktop.ini and thumbs.db which have special meaning to the Windows shell. |
| 138 | // Even on other platforms, this will return whether or not a file name is |
| 139 | // reserved on Windows. |
| 140 | NET_EXPORT bool IsReservedNameOnWindows( |
| 141 | const base::FilePath::StringType& filename); |
| 142 | |
[email protected] | d96cf75 | 2014-04-09 04:05:28 | [diff] [blame] | 143 | } // namespace net |
| 144 | |
| 145 | #endif // NET_BASE_FILENAME_UTIL_H_ |