http/download.v: replace C code with V + clean up

pull/480/head
Alexander Medvednikov 2019-06-23 23:02:11 +02:00
parent b6948ad9af
commit 35cef79149
3 changed files with 25 additions and 88 deletions

View File

@ -1,52 +0,0 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module http
import os
type downloadfn fn (written int)
struct DownloadStruct {
stream voidptr
written int
cb downloadfn
}
fn download_cb(ptr voidptr, size, nmemb size_t, userp voidptr) int {
data := &DownloadStruct(userp)
# size_t written = fwrite(ptr, size, nmemb, (FILE*)(data->stream));
# data->written += written;
if !isnil(data.cb) {
# data->cb(data->written);
}
# return written;
return 0
}
fn download_file_with_progress(url, out string, cb, cb_finished voidptr) {
curl := C.curl_easy_init()
if isnil(curl) {
return
}
# FILE* fp = fopen(out.str,"wb");
# curl_easy_setopt(curl, CURLOPT_URL, url.str);
C.curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, download_cb)
data := &DownloadStruct {
// stream:fp
cb: cb
}
# data->stream = fp;
# curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);
# double d = 0;
# curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
# CURLcode res = curl_easy_perform(curl);
# curl_easy_cleanup(curl);
# fclose(fp);
# void (*finished)() =cb_finished; finished();
}
fn download_file(url, out string) {
}

View File

@ -6,63 +6,51 @@ module http
import os import os
struct LUEL {
age int
}
type downloadfn fn (written int) type downloadfn fn (written int)
type download_finished_fn fn ()
struct DownloadStruct { struct DownloadStruct {
mut:
stream voidptr stream voidptr
written int written int
cb downloadfn cb downloadfn
} }
fn download_cb(ptr voidptr, size, nmemb size_t, userp voidptr) int { fn download_cb(ptr voidptr, size, nmemb size_t, userp voidptr) int {
// # struct http__MemoryStruct *mem = (struct http__MemoryStruct *)userp; mut data := &DownloadStruct(userp)
data := &DownloadStruct(userp) written := C.fwrite(ptr, size, nmemb, data.stream)
# size_t written = fwrite(ptr, size, nmemb, (FILE*)(data->stream)); data.written += written
// # printf("!!!%d\n", written); #data->cb(data->written); // TODO
# data->written += written; return written
if !isnil(data.cb) {
# data->cb(data->written);
}
# return written;
return 0
} }
fn download_file_with_progress(url, out string, cb, cb_finished voidptr) { fn download_file_with_progress(url, out string, cb, cb_finished download_finished_fn) {
/*
curl := C.curl_easy_init() curl := C.curl_easy_init()
if isnil(curl) { if isnil(curl) {
return return
} }
# FILE* fp = fopen(out.str,"wb"); cout := out.cstr()
# curl_easy_setopt(curl, CURLOPT_URL, url.str); fp := C.fopen(cout, 'wb')
C.curl_easy_setopt(curl, CURLOPT_URL, url.cstr())
C.curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, download_cb) C.curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, download_cb)
// # curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http__download_cb);
data := &DownloadStruct { data := &DownloadStruct {
// stream:fp stream:fp
cb: cb cb: cb
} }
# data->stream = fp; C.curl_easy_setopt(curl, CURLOPT_WRITEDATA, data)
# curl_easy_setopt(curl, CURLOPT_WRITEDATA, data); mut d := 0.0
# double d = 0; C.curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)
# curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d); C.curl_easy_perform(curl)
# CURLcode res = curl_easy_perform(curl); C.curl_easy_cleanup(curl)
println('DONE!') C.fclose(fp)
# curl_easy_cleanup(curl); #cb_finished(); // TODO
# fclose(fp);
# void (*finished)() =cb_finished; finished();
*/
} }
fn download_file(url, out string) { fn download_file(url, out string) {
// println('\nDOWNLOAD FILE $out url=$url') download_file_with_progress(url, out, empty, empty)
// -L follow redirects
// println('curl -L -o "$out" "$url"')
os.system2('curl -s -L -o "$out" "$url"')
// res := os.system('curl -s -L -o "$out" "$url"')
// println(res)
} }
fn empty() {
}

View File

@ -37,6 +37,7 @@ import const (
CURLOPT_POSTFIELDS CURLOPT_POSTFIELDS
CURLOPT_CUSTOMREQUEST CURLOPT_CUSTOMREQUEST
CURLOPT_TCP_KEEPALIVE CURLOPT_TCP_KEEPALIVE
CURLINFO_CONTENT_LENGTH_DOWNLOAD
CURLE_OK CURLE_OK
) )