MCPcopy Create free account
hub / github.com/altairwei/WizNotePlus / WizURLDownloadToFile

Function WizURLDownloadToFile

src/share/WizMisc.cpp:3022–3080  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3020
3021
3022bool WizURLDownloadToFile(const QString& url, const QString& fileName, bool isImage)
3023{
3024 QString newUrl = url;
3025 QNetworkAccessManager netCtrl;
3026 QNetworkReply* reply;
3027
3028 // Get data from URL
3029 bool redirect = false;
3030 QByteArray byData;
3031 do
3032 {
3033 QNetworkRequest request(newUrl);
3034 request.setAttribute(
3035 QNetworkRequest::RedirectPolicyAttribute,
3036 QNetworkRequest::NoLessSafeRedirectPolicy);
3037 //
3038 reply = netCtrl.get(request);
3039 WizAutoTimeOutEventLoop loop(reply);
3040 loop.setTimeoutWaitSeconds(NETWORK_TIMEOUT_SECS);
3041 loop.exec();
3042
3043 if (loop.error() != QNetworkReply::NoError)
3044 {
3045 return false;
3046 }
3047 //
3048 if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 301) {
3049 redirect = true;
3050 QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
3051 newUrl = redirectUrl.toString();
3052 } else {
3053 redirect = false;
3054 byData = loop.result();
3055 }
3056 //
3057 }
3058 while (redirect);
3059
3060 WizDeleteFile(fileName);
3061
3062 /*
3063 // It's not necessary to treat image specially?
3064 if (isImage)
3065 {
3066 QPixmap pix;
3067 pix.loadFromData(byData);
3068 return pix.save(fileName);
3069 }
3070 */
3071
3072 // Save data to file
3073 QFile file(fileName);
3074 if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
3075 return false;
3076 file.write(byData);
3077 file.close();
3078
3079 return true;

Callers 4

downloadImageMethod · 0.85
URLDownloadToFileMethod · 0.85
downloadMethod · 0.85

Calls 10

WizDeleteFileFunction · 0.85
setTimeoutWaitSecondsMethod · 0.80
errorMethod · 0.80
resultMethod · 0.80
getMethod · 0.45
execMethod · 0.45
toStringMethod · 0.45
openMethod · 0.45
writeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected