hc
2024-05-16 8d2a02b24d66aa359e83eebc1ed3c0f85367a1cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*************************************************************************
   > File Name: download.cpp
   > Author: jkand.huang
   > Mail: jkand.huang@rock-chips.com
   > Created Time: Thu 07 Mar 2019 10:30:31 AM CST
 ************************************************************************/
 
#include <stdio.h>
#include <curl/curl.h>
#include "log.h"
size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    return fwrite(ptr, size, nmemb, stream);
}
 
extern double processvalue;
 
int my_progress_func(char *progress_data,
                     double t, /* dltotal */
                     double d, /* dlnow */
                     double ultotal,
                     double ulnow)
{
    processvalue = ulnow / ultotal * 100 / 110;
    return 0;
}
 
int download_file(char *url, char *output_filename)
{
    CURL *curl;
    CURLcode res;
    FILE *outfile;
    char *progress_data = "* ";
 
    curl = curl_easy_init();
    if(curl)
    {
        outfile = fopen(output_filename, "wb");
 
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
        curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func);
        curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, progress_data);
 
        res = curl_easy_perform(curl);
 
        if(res != CURLE_OK)
            LOGE("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        fclose(outfile);
        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    if(res != CURLE_OK){
        LOGE("download Error.\n");
        return -1;
    }
    return 0;
}