Skip to content

Commit

Permalink
http/download.v: replace C code with V + clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Jun 23, 2019
1 parent b6948ad commit 35cef79
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 88 deletions.
52 changes: 0 additions & 52 deletions http/download_lin.v

This file was deleted.

60 changes: 24 additions & 36 deletions http/download_mac.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,51 @@ module http

import os

struct LUEL {
age int
}

type downloadfn fn (written int)
type download_finished_fn fn ()

struct DownloadStruct {
mut:
stream voidptr
written int
cb downloadfn
}

fn download_cb(ptr voidptr, size, nmemb size_t, userp voidptr) int {
// # struct http__MemoryStruct *mem = (struct http__MemoryStruct *)userp;
data := &DownloadStruct(userp)
# size_t written = fwrite(ptr, size, nmemb, (FILE*)(data->stream));
// # printf("!!!%d\n", written);
# data->written += written;
if !isnil(data.cb) {
# data->cb(data->written);
}
# return written;
return 0
mut data := &DownloadStruct(userp)
written := C.fwrite(ptr, size, nmemb, data.stream)
data.written += written
#data->cb(data->written); // TODO
return written
}

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()
if isnil(curl) {
return
}
# FILE* fp = fopen(out.str,"wb");
# curl_easy_setopt(curl, CURLOPT_URL, url.str);
cout := out.cstr()
fp := C.fopen(cout, 'wb')
C.curl_easy_setopt(curl, CURLOPT_URL, url.cstr())
C.curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, download_cb)
// # curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http__download_cb);
data := &DownloadStruct {
// stream:fp
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);
println('DONE!')
# curl_easy_cleanup(curl);
# fclose(fp);
# void (*finished)() =cb_finished; finished();
*/
C.curl_easy_setopt(curl, CURLOPT_WRITEDATA, data)
mut d := 0.0
C.curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)
C.curl_easy_perform(curl)
C.curl_easy_cleanup(curl)
C.fclose(fp)
#cb_finished(); // TODO
}

fn download_file(url, out string) {
// println('\nDOWNLOAD FILE $out url=$url')
// -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)
download_file_with_progress(url, out, empty, empty)
}

fn empty() {

}

1 change: 1 addition & 0 deletions http/http_mac.v
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import const (
CURLOPT_POSTFIELDS
CURLOPT_CUSTOMREQUEST
CURLOPT_TCP_KEEPALIVE
CURLINFO_CONTENT_LENGTH_DOWNLOAD
CURLE_OK
)

Expand Down

4 comments on commit 35cef79

@microcai
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow , amazing !

@alienzj
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow , amazing !

@Invincibl-e
Copy link

@Invincibl-e Invincibl-e commented on 35cef79 Jun 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WOW, Amazing!

@imtsuki
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WOW, Amazing!

Please sign in to comment.