OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/filename_util.h" | 5 #include "net/base/filename_util.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 ReplaceIllegalCharactersCallback replace_illegal_characters_callback) { | 222 ReplaceIllegalCharactersCallback replace_illegal_characters_callback) { |
223 // TODO: this function to be updated to match the httpbis recommendations. | 223 // TODO: this function to be updated to match the httpbis recommendations. |
224 // Talk to abarth for the latest news. | 224 // Talk to abarth for the latest news. |
225 | 225 |
226 // We don't translate this fallback string, "download". If localization is | 226 // We don't translate this fallback string, "download". If localization is |
227 // needed, the caller should provide localized fallback in |default_name|. | 227 // needed, the caller should provide localized fallback in |default_name|. |
228 static const base::FilePath::CharType kFinalFallbackName[] = | 228 static const base::FilePath::CharType kFinalFallbackName[] = |
229 FILE_PATH_LITERAL("download"); | 229 FILE_PATH_LITERAL("download"); |
230 std::string filename; // In UTF-8 | 230 std::string filename; // In UTF-8 |
231 bool overwrite_extension = false; | 231 bool overwrite_extension = false; |
232 | 232 bool is_name_from_content_disposition = false; |
233 // Try to extract a filename from content-disposition first. | 233 // Try to extract a filename from content-disposition first. |
234 if (!content_disposition.empty()) { | 234 if (!content_disposition.empty()) { |
235 HttpContentDisposition header(content_disposition, referrer_charset); | 235 HttpContentDisposition header(content_disposition, referrer_charset); |
236 filename = header.filename(); | 236 filename = header.filename(); |
| 237 if (!filename.empty()) |
| 238 is_name_from_content_disposition = true; |
237 } | 239 } |
238 | 240 |
239 // Then try to use the suggested name. | 241 // Then try to use the suggested name. |
240 if (filename.empty() && !suggested_name.empty()) | 242 if (filename.empty() && !suggested_name.empty()) |
241 filename = suggested_name; | 243 filename = suggested_name; |
242 | 244 |
243 // Now try extracting the filename from the URL. GetFileNameFromURL() only | 245 // Now try extracting the filename from the URL. GetFileNameFromURL() only |
244 // looks at the last component of the URL and doesn't return the hostname as a | 246 // looks at the last component of the URL and doesn't return the hostname as a |
245 // failover. | 247 // failover. |
246 if (filename.empty()) | 248 if (filename.empty()) |
(...skipping 21 matching lines...) Expand all Loading... |
268 SanitizeGeneratedFileName(&result_str, replace_trailing); | 270 SanitizeGeneratedFileName(&result_str, replace_trailing); |
269 if (result_str.find_last_not_of(FILE_PATH_LITERAL("-_")) == | 271 if (result_str.find_last_not_of(FILE_PATH_LITERAL("-_")) == |
270 base::FilePath::StringType::npos) { | 272 base::FilePath::StringType::npos) { |
271 result_str = !default_name_str.empty() | 273 result_str = !default_name_str.empty() |
272 ? default_name_str | 274 ? default_name_str |
273 : base::FilePath::StringType(kFinalFallbackName); | 275 : base::FilePath::StringType(kFinalFallbackName); |
274 overwrite_extension = false; | 276 overwrite_extension = false; |
275 } | 277 } |
276 replace_illegal_characters_callback.Run(&result_str, '-'); | 278 replace_illegal_characters_callback.Run(&result_str, '-'); |
277 base::FilePath result(result_str); | 279 base::FilePath result(result_str); |
278 GenerateSafeFileName(mime_type, overwrite_extension, &result); | 280 // extension should not appended to filename derived from |
| 281 // content-disposition, if it does not have one. |
| 282 // Hence mimetype and overwrite_extension values are not used. |
| 283 if (is_name_from_content_disposition) |
| 284 GenerateSafeFileName("", false, &result); |
| 285 else |
| 286 GenerateSafeFileName(mime_type, overwrite_extension, &result); |
279 | 287 |
280 base::string16 result16; | 288 base::string16 result16; |
281 if (!FilePathToString16(result, &result16)) { | 289 if (!FilePathToString16(result, &result16)) { |
282 result = base::FilePath(default_name_str); | 290 result = base::FilePath(default_name_str); |
283 if (!FilePathToString16(result, &result16)) { | 291 if (!FilePathToString16(result, &result16)) { |
284 result = base::FilePath(kFinalFallbackName); | 292 result = base::FilePath(kFinalFallbackName); |
285 FilePathToString16(result, &result16); | 293 FilePathToString16(result, &result16); |
286 } | 294 } |
287 } | 295 } |
288 return result16; | 296 return result16; |
(...skipping 22 matching lines...) Expand all Loading... |
311 base::FilePath generated_name( | 319 base::FilePath generated_name( |
312 base::SysWideToNativeMB(base::UTF16ToWide(file_name))); | 320 base::SysWideToNativeMB(base::UTF16ToWide(file_name))); |
313 #endif | 321 #endif |
314 | 322 |
315 DCHECK(!generated_name.empty()); | 323 DCHECK(!generated_name.empty()); |
316 | 324 |
317 return generated_name; | 325 return generated_name; |
318 } | 326 } |
319 | 327 |
320 } // namespace net | 328 } // namespace net |
OLD | NEW |