Specify thumbnail format from response header (#16352)

* Specify thumbnail format from response header
This commit is contained in:
Sangwoo Ko
2022-12-15 12:28:03 +09:00
committed by GitHub
parent 3a8bc610a0
commit e347c153ae
4 changed files with 88 additions and 6 deletions
@@ -252,8 +252,23 @@ void APIRequestHelper::OnResponse(
void APIRequestHelper::OnDownload(SimpleURLLoaderList::iterator iter,
DownloadCallback callback,
base::FilePath path) {
auto* loader = iter->get();
base::flat_map<std::string, std::string> headers;
if (loader->ResponseInfo()) {
auto headers_list = loader->ResponseInfo()->headers;
if (headers_list) {
size_t header_iter = 0;
std::string key;
std::string value;
while (headers_list->EnumerateHeaderLines(&header_iter, &key, &value)) {
key = base::ToLowerASCII(key);
headers[key] = value;
}
}
}
url_loaders_.erase(iter);
std::move(callback).Run(path);
std::move(callback).Run(path, std::move(headers));
}
} // namespace api_request_helper
@@ -104,7 +104,9 @@ class APIRequestHelper {
size_t max_body_size = -1u,
ResponseConversionCallback conversion_callback = base::NullCallback());
using DownloadCallback = base::OnceCallback<void(base::FilePath)>;
using DownloadCallback = base::OnceCallback<void(
base::FilePath,
const base::flat_map<std::string, std::string>& /*response_headers*/)>;
Ticket Download(const GURL& url,
const std::string& payload,
const std::string& payload_content_type,
@@ -7,6 +7,8 @@
#include <utility>
#include "base/files/file_util.h"
#include "base/task/thread_pool.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "services/network/public/cpp/resource_request.h"
@@ -77,10 +79,54 @@ void PlaylistThumbnailDownloader::CancelAllDownloadRequests() {
ticket_map_.clear();
}
void PlaylistThumbnailDownloader::OnThumbnailDownloaded(const std::string& id,
base::FilePath path) {
void PlaylistThumbnailDownloader::OnThumbnailDownloaded(
const std::string& id,
base::FilePath path,
const base::flat_map<std::string, std::string>& response_headers) {
VLOG(2) << __func__ << " id: " << id;
#if BUILDFLAG(IS_ANDROID)
if (!path.empty()) {
// Anroid requires spedific format for thumbnail file.
std::string extension = "png";
if (response_headers.contains("content-type")) {
std::string content_type = response_headers.at("content-type");
if (base::StartsWith(content_type, "image/")) {
extension = content_type.substr(6);
}
}
RenameFilePerFormat(id, path, extension);
return;
}
#endif
DCHECK(!ticket_map_.empty());
ticket_map_.erase(id);
delegate_->OnThumbnailDownloaded(id, path);
}
#if BUILDFLAG(IS_ANDROID)
void PlaylistThumbnailDownloader::RenameFilePerFormat(
const std::string& id,
const base::FilePath& path,
const std::string& extension) {
if (!task_runner_) {
task_runner_ = base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
}
base::FilePath new_path = path.AddExtension(extension);
task_runner_->PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&base::ReplaceFile, path, new_path, nullptr),
base::BindOnce(&PlaylistThumbnailDownloader::OnRenameFilePerFormat,
base::Unretained(this), id, new_path));
}
void PlaylistThumbnailDownloader::OnRenameFilePerFormat(
const std::string& id,
const base::FilePath& new_path,
bool result) {
DCHECK(!ticket_map_.empty());
ticket_map_.erase(id);
delegate_->OnThumbnailDownloaded(
id, result ? new_path : new_path.RemoveExtension());
}
#endif
@@ -22,6 +22,12 @@ namespace network {
class SharedURLLoaderFactory;
} // namespace network
#if BUILDFLAG(IS_ANDROID)
namespace base {
class SequencedTaskRunner;
} // namespace base
#endif
class GURL;
class PlaylistThumbnailDownloader {
@@ -36,7 +42,6 @@ class PlaylistThumbnailDownloader {
PlaylistThumbnailDownloader(content::BrowserContext* context,
Delegate* delegate);
virtual ~PlaylistThumbnailDownloader();
PlaylistThumbnailDownloader(const PlaylistThumbnailDownloader&) = delete;
PlaylistThumbnailDownloader& operator=(const PlaylistThumbnailDownloader&) =
delete;
@@ -51,7 +56,21 @@ class PlaylistThumbnailDownloader {
using APIRequestHelper = api_request_helper::APIRequestHelper;
using TicketMap = base::flat_map<std::string, APIRequestHelper::Ticket>;
void OnThumbnailDownloaded(const std::string& id, base::FilePath path);
void OnThumbnailDownloaded(
const std::string& id,
base::FilePath path,
const base::flat_map<std::string, std::string>& response_headers);
#if BUILDFLAG(IS_ANDROID)
void RenameFilePerFormat(const std::string& id,
const base::FilePath& path,
const std::string& extension);
void OnRenameFilePerFormat(const std::string& id,
const base::FilePath& new_path,
bool result);
scoped_refptr<base::SequencedTaskRunner> task_runner_;
#endif
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;