2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-05-03 09:01:06 +04:30
|
|
|
#include <AK/NumberFormat.h>
|
2020-01-01 18:53:34 +01:00
|
|
|
#include <AK/SharedBuffer.h>
|
2020-05-03 09:01:06 +04:30
|
|
|
#include <AK/URL.h>
|
2020-08-05 20:53:16 +02:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/EventLoop.h>
|
2019-11-23 21:48:39 +01:00
|
|
|
#include <LibProtocol/Client.h>
|
2019-11-24 13:20:44 +01:00
|
|
|
#include <LibProtocol/Download.h>
|
2019-11-23 21:48:39 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-08-05 20:53:16 +02:00
|
|
|
const char* url_str = nullptr;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_positional_argument(url_str, "URL to download from", "url");
|
|
|
|
args_parser.parse(argc, argv);
|
2019-11-23 22:16:23 +01:00
|
|
|
|
2020-08-05 20:53:16 +02:00
|
|
|
URL url(url_str);
|
2019-11-23 22:16:23 +01:00
|
|
|
if (!url.is_valid()) {
|
2020-08-05 20:53:16 +02:00
|
|
|
fprintf(stderr, "'%s' is not a valid URL\n", url_str);
|
2019-11-23 22:16:23 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::EventLoop loop;
|
2020-02-05 18:21:30 +01:00
|
|
|
auto protocol_client = Protocol::Client::construct();
|
2019-11-23 21:48:39 +01:00
|
|
|
|
2019-11-24 13:20:44 +01:00
|
|
|
auto download = protocol_client->start_download(url.to_string());
|
2020-04-04 20:00:07 +02:00
|
|
|
if (!download) {
|
2020-08-05 20:53:16 +02:00
|
|
|
fprintf(stderr, "Failed to start download for '%s'\n", url_str);
|
2020-04-04 20:00:07 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2020-05-03 09:01:06 +04:30
|
|
|
u32 previous_downloaded_size { 0 };
|
|
|
|
timeval prev_time, current_time, time_diff;
|
|
|
|
gettimeofday(&prev_time, nullptr);
|
|
|
|
|
|
|
|
download->on_progress = [&](Optional<u32> maybe_total_size, u32 downloaded_size) {
|
|
|
|
fprintf(stderr, "\r\033[2K");
|
2020-05-30 22:17:46 +02:00
|
|
|
if (maybe_total_size.has_value()) {
|
|
|
|
fprintf(stderr, "\033]9;%d;%d;\033\\", downloaded_size, maybe_total_size.value());
|
2020-05-03 09:01:06 +04:30
|
|
|
fprintf(stderr, "Download progress: %s / %s", human_readable_size(downloaded_size).characters(), human_readable_size(maybe_total_size.value()).characters());
|
2020-05-30 22:17:46 +02:00
|
|
|
} else {
|
2020-05-03 09:01:06 +04:30
|
|
|
fprintf(stderr, "Download progress: %s / ???", human_readable_size(downloaded_size).characters());
|
2020-05-30 22:17:46 +02:00
|
|
|
}
|
2020-05-03 09:01:06 +04:30
|
|
|
|
|
|
|
gettimeofday(¤t_time, nullptr);
|
|
|
|
timersub(¤t_time, &prev_time, &time_diff);
|
|
|
|
|
|
|
|
auto time_diff_ms = time_diff.tv_sec * 1000 + time_diff.tv_usec / 1000;
|
|
|
|
auto size_diff = downloaded_size - previous_downloaded_size;
|
|
|
|
|
|
|
|
fprintf(stderr, " at %s/s", human_readable_size(((float)size_diff / (float)time_diff_ms) * 1000).characters());
|
|
|
|
|
|
|
|
previous_downloaded_size = downloaded_size;
|
|
|
|
prev_time = current_time;
|
2019-11-23 21:48:39 +01:00
|
|
|
};
|
2020-06-13 22:19:34 +02:00
|
|
|
download->on_finish = [&](bool success, auto& payload, auto, auto&, auto) {
|
2020-05-30 22:17:46 +02:00
|
|
|
fprintf(stderr, "\033]9;-1;\033\\");
|
2020-05-03 09:01:06 +04:30
|
|
|
fprintf(stderr, "\n");
|
2019-11-24 13:20:44 +01:00
|
|
|
if (success)
|
|
|
|
write(STDOUT_FILENO, payload.data(), payload.size());
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Download failed :(\n");
|
|
|
|
loop.quit(0);
|
2019-11-23 21:48:39 +01:00
|
|
|
};
|
2019-11-24 13:20:44 +01:00
|
|
|
dbgprintf("started download with id %d\n", download->id());
|
2019-11-23 21:48:39 +01:00
|
|
|
|
|
|
|
return loop.exec();
|
|
|
|
}
|