Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download: If we have a last-modified date, set the mtime of the target file #36

Merged
merged 1 commit into from Nov 17, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -29,6 +29,8 @@ import std.range : chain;
import std.algorithm : startsWith;
import std.array : appender;
import std.path : buildPath, dirName, buildNormalizedPath;
import std.typecons : Nullable;
import std.datetime : Clock, parseRFC822DateTime, SysTime;
static import std.file;

import asgen.logging;
@@ -348,13 +350,15 @@ bool isRemote (const string uri)
return (!match.empty);
}

private void download (const string url, ref File dest, const uint retryCount = 5) @trusted
private immutable(Nullable!SysTime) download (const string url, ref File dest, const uint retryCount = 5) @trusted
in { assert (url.isRemote); }
body
{
import core.time;
import std.net.curl : CurlException, HTTP, FTP;

Nullable!SysTime ret;

size_t onReceiveCb (File f, ubyte[] data)
{
f.rawWrite (data);
@@ -370,6 +374,10 @@ body
downloader.dataTimeout = dur!"seconds" (30);
downloader.onReceive = (data) => onReceiveCb (dest, data);
downloader.perform();
if ("last-modified" in downloader.responseHeaders) {
Copy link
Owner

Choose a reason for hiding this comment

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

If you want, you can optimize this slightly like this:

immutable lastModifiedP = "last-modified" in downloader.responseHeaders;
if (lastModifiedP !is null) {
    auto lastmodified = *lastModifiedP;
    ret = parseRFC822DateTime(lastmodified);
}

=> one lookup instead of two.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I hope that hashing is super fast (and maybe this could be optimised anyway), so this shouldn't really matter...

Copy link
Owner

Choose a reason for hiding this comment

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

It is very fast and I assume the hash table is also pretty small, so this is indeed a micro optimization. Not sure what the compiler does here and how smart it is in optimizing - would be pretty cool if it did optimize that bit :-)

Anyway, this comment was from the "pedantic" and maybe-do-it class of comments ^^

auto lastmodified = downloader.responseHeaders["last-modified"];
ret = parseRFC822DateTime(lastmodified);
}
} else {
auto downloader = FTP (url);
downloader.connectTimeout = dur!"seconds" (30);
@@ -389,6 +397,8 @@ body
throw e;
}
}

return ret;
}

/**
@@ -449,10 +459,13 @@ body
mkdirRecurse (dest.dirName);

auto f = File (dest, "wb");
scope (exit) f.close ();
scope (failure) remove (dest);

download (url, f, retryCount);
auto time = download (url, f, retryCount);

f.close ();
if (!time.isNull)
setTimes (dest, Clock.currTime, time);
}

/**