Skip to content

Commit

Permalink
Fix GOG download filenames (tkashkin#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkashkin committed Mar 6, 2020
1 parent 2c73d30 commit 1275a10
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
32 changes: 26 additions & 6 deletions src/data/sources/gog/GOGGame.vala
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ namespace GameHub.Data.Sources.GOG
var checksum_url = root.get_string_member("checksum");
var remote = File.new_for_uri(url);

var local = installers_dir.get_child("gog_" + game.id + "_" + this.id + "_" + id);
string? local_filename = null;

string? hash = null;
var hash_type = ChecksumType.MD5;
Expand All @@ -653,11 +653,21 @@ namespace GameHub.Data.Sources.GOG
if(checksum_file_node != null)
{
hash = checksum_file_node->get_prop("md5");
local_filename = checksum_file_node->get_prop("name");
}

delete checksum_root;
}

if(local_filename == null && "/namespaces/website/download?path=" in url)
{
var remote_path_encoded = url.split("/namespaces/website/download?path=")[1].split("&")[0];
var remote_path = Uri.unescape_string(remote_path_encoded);
local_filename = File.new_for_path(remote_path).get_basename();
}

var local = installers_dir.get_child(local_filename ?? "gog_" + game.id + "_" + this.id + "_" + id);

parts.add(new Runnable.DownloadableInstaller.Part(id, url, size, remote, local, hash, hash_type));
}
}
Expand Down Expand Up @@ -789,7 +799,7 @@ namespace GameHub.Data.Sources.GOG
size = json.get_int_member("total_size");
dl_info = new Downloader.DownloadInfo(text, game.name, game.icon, null, null, icon);

filename = "gog_" + game.id + "_bonus_" + id;
filename = @"gog_$(game.id)_bonus_$(id)";
if(bonus_map != null && bonus_map.has_member(id))
{
filename = bonus_map.get_string_member(id);
Expand All @@ -800,6 +810,8 @@ namespace GameHub.Data.Sources.GOG

public async File? download()
{
if(game.bonus_content_dir == null) return null;

Json.Node? root_node = null;

while(true)
Expand All @@ -819,16 +831,24 @@ namespace GameHub.Data.Sources.GOG
var root = root_node.get_object();
if(root == null || !root.has_member("downlink")) return null;

var link = root.get_string_member("downlink");
var remote = File.new_for_uri(link);
var url = root.get_string_member("downlink");
var checksum_url = root.get_string_member("checksum");
var remote = File.new_for_uri(url);

if(game.bonus_content_dir == null) return null;
if(filename == @"gog_$(game.id)_bonus_$(id)" && "/namespaces/website/download?path=" in url)
{
var remote_path_encoded = url.split("/namespaces/website/download?path=")[1].split("&")[0];
var remote_path = Uri.unescape_string(remote_path_encoded);
filename = File.new_for_path(remote_path).get_basename();
}

var local = game.bonus_content_dir.get_child(filename);
warning("[filename] %s", filename);

FSUtils.mkdir(FSUtils.Paths.GOG.Games);
FSUtils.mkdir(game.bonus_content_dir.get_path());

var local = game.bonus_content_dir.get_child(filename);

status = new BonusContent.Status(BonusContent.State.DOWNLOADING, null);
var ds_id = Downloader.download_manager().file_download_started.connect(dl => {
if(dl.remote != remote) return;
Expand Down
42 changes: 22 additions & 20 deletions src/utils/downloader/SoupDownloader.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ namespace GameHub.Utils.Downloader.SoupDownloader
private HashTable<string, DownloadInfo> dl_info;
private ArrayQueue<string> dl_queue;

private static string[] supported_schemes = { "http", "https" };
private static string[] URL_SCHEMES = { "http", "https" };
private static string[] FILENAME_BLACKLIST = { "download" };

public SoupDownloader()
{
Expand Down Expand Up @@ -112,7 +113,7 @@ namespace GameHub.Utils.Downloader.SoupDownloader

try
{
if(remote.get_uri_scheme() in supported_schemes)
if(remote.get_uri_scheme() in URL_SCHEMES)
yield download_from_http(download, preserve_filename, queue);
else
yield download_from_filesystem(download);
Expand Down Expand Up @@ -239,29 +240,30 @@ namespace GameHub.Utils.Downloader.SoupDownloader
filename = download.remote.get_basename();
}

if(filename != null)
if(filename != null && !(filename in FILENAME_BLACKLIST))
{
download.local = download.local.get_parent().get_child(filename);
if(download.local.query_exists())
{
if(GameHub.Application.log_downloader)
{
debug(@"[SoupDownloader] '%s' exists", download.local.get_path());
}
var info = download.local.query_info(FileAttribute.STANDARD_SIZE, FileQueryInfoFlags.NONE);
if(info.get_size() == dl_bytes_total)
{
session.cancel_message(msg, Status.OK);
return;
}
}
if(GameHub.Application.log_downloader)
{
debug(@"[SoupDownloader] Downloading to '%s'", download.local.get_path());
}
}
}

if(download.local.query_exists())
{
if(GameHub.Application.log_downloader)
{
debug(@"[SoupDownloader] '%s' exists", download.local.get_path());
}
var info = download.local.query_info(FileAttribute.STANDARD_SIZE, FileQueryInfoFlags.NONE);
if(info.get_size() == dl_bytes_total)
{
session.cancel_message(msg, Status.OK);
return;
}
}
if(GameHub.Application.log_downloader)
{
debug(@"[SoupDownloader] Downloading to '%s'", download.local.get_path());
}

#if SOUP_2_60
int64 rstart = -1, rend = -1;
if(resume_dl && msg.response_headers.get_content_range(out rstart, out rend, out dl_bytes_total))
Expand Down

0 comments on commit 1275a10

Please sign in to comment.