{"action":"created","comment":{"url":"https://api.github.com/repos/oberien/pundoc/pulls/comments/227319046","pull_request_review_id":167308220,"id":227319046,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIyNzMxOTA0Ng==","diff_hunk":"@@ -0,0 +1,141 @@\n+use std::fs::{File, OpenOptions, create_dir_all, remove_file};\n+use std::io;\n+use std::path::{Path, PathBuf};\n+\n+// TODO: The error representation is awkward with reqwest, evaluate cHTTP instead.\n+use reqwest::{Client, Error as RequestError, RedirectPolicy, Response, header};\n+use url::Url;\n+\n+/// Provide access to remotely hosted resources.\n+pub struct Remote {\n+    client: Client,\n+    temp: PathBuf,\n+}\n+\n+pub struct Downloaded {\n+    file: File,\n+    path: PathBuf,\n+    content_type: Option<ContentType>,\n+}\n+\n+#[derive(Debug)]\n+pub enum Error {\n+    Request(RequestError),\n+    Io(io::Error),\n+}\n+\n+#[derive(Clone, Copy, Debug, Eq, PartialEq)]\n+pub enum ContentType {\n+    Markdown,\n+    Image,\n+    Pdf,\n+}\n+\n+impl Remote {\n+    pub fn new(download_folder: PathBuf) -> Result<Self, Error> {\n+        create_dir_all(&download_folder)?;\n+\n+        let client = Client::builder()\n+            // TODO: how should redirects interact relative references etc. ?\n+            .redirect(RedirectPolicy::none())\n+            .build()?;\n+\n+        Ok(Remote {\n+            temp: download_folder,\n+            client,\n+        })\n+    }\n+\n+    pub fn http(&self, url: Url) -> Result<Downloaded, Error> {\n+        let mut response = self.client.get(url.as_ref())\n+            // TODO set headers: user agent, accepted content type, ...\n+            .send()\n+            .and_then(|response| response.error_for_status())?;\n+\n+        let path = self.target_path(&url);\n+\n+        create_dir_all(path.parent().unwrap())?;\n+\n+        // Replace whatever file already existed.\n+        // TODO: proper caching\n+        let _ = remove_file(&path);\n+        let mut file = OpenOptions::new()\n+            .write(true)\n+            .create_new(true)\n+            .open(&path)?;\n+\n+        io::copy(&mut response, &mut file)?;\n+\n+        let content_type = self.content_type(&response);\n+\n+        Ok(Downloaded {\n+            file,\n+            path,\n+            content_type,\n+        })\n+    }\n+\n+    fn content_type(&self, response: &Response) -> Option<ContentType> {\n+        // Get a value content type header if any.\n+        let header = response.headers().get(header::CONTENT_TYPE);\n+        let header = header.and_then(|value| value.to_str().ok());\n+\n+        match header {\n+            Some(\"text/markdown\") => Some(ContentType::Markdown),\n+            Some(\"image/png\") | Some(\"image/jpeg\") => Some(ContentType::Image),\n+            Some(\"application/pdf\") => Some(ContentType::Pdf),","path":"src/resolve/remote.rs","position":86,"original_position":86,"commit_id":"64b7277239f2bfbbfb0d1d2b3fd1b5bff5bbf21a","original_commit_id":"64b7277239f2bfbbfb0d1d2b3fd1b5bff5bbf21a","user":{"login":"oberien","id":4820508,"node_id":"MDQ6VXNlcjQ4MjA1MDg=","avatar_url":"https://avatars1.githubusercontent.com/u/4820508?v=4","gravatar_id":"","url":"https://api.github.com/users/oberien","html_url":"https://github.com/oberien","followers_url":"https://api.github.com/users/oberien/followers","following_url":"https://api.github.com/users/oberien/following{/other_user}","gists_url":"https://api.github.com/users/oberien/gists{/gist_id}","starred_url":"https://api.github.com/users/oberien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oberien/subscriptions","organizations_url":"https://api.github.com/users/oberien/orgs","repos_url":"https://api.github.com/users/oberien/repos","events_url":"https://api.github.com/users/oberien/events{/privacy}","received_events_url":"https://api.github.com/users/oberien/received_events","type":"User","site_admin":false},"body":"Can we use the `mime` crate here?","created_at":"2018-10-23T10:03:56Z","updated_at":"2018-10-23T10:15:33Z","html_url":"https://github.com/oberien/pundoc/pull/4#discussion_r227319046","pull_request_url":"https://api.github.com/repos/oberien/pundoc/pulls/4","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/oberien/pundoc/pulls/comments/227319046"},"html":{"href":"https://github.com/oberien/pundoc/pull/4#discussion_r227319046"},"pull_request":{"href":"https://api.github.com/repos/oberien/pundoc/pulls/4"}}},"pull_request":{"url":"https://api.github.com/repos/oberien/pundoc/pulls/4","id":224764723,"node_id":"MDExOlB1bGxSZXF1ZXN0MjI0NzY0NzIz","html_url":"https://github.com/oberien/pundoc/pull/4","diff_url":"https://github.com/oberien/pundoc/pull/4.diff","patch_url":"https://github.com/oberien/pundoc/pull/4.patch","issue_url":"https://api.github.com/repos/oberien/pundoc/issues/4","number":4,"state":"open","locked":false,"title":"Implements downloading of included files","user":{"login":"HeroicKatora","id":5550310,"node_id":"MDQ6VXNlcjU1NTAzMTA=","avatar_url":"https://avatars2.githubusercontent.com/u/5550310?v=4","gravatar_id":"","url":"https://api.github.com/users/HeroicKatora","html_url":"https://github.com/HeroicKatora","followers_url":"https://api.github.com/users/HeroicKatora/followers","following_url":"https://api.github.com/users/HeroicKatora/following{/other_user}","gists_url":"https://api.github.com/users/HeroicKatora/gists{/gist_id}","starred_url":"https://api.github.com/users/HeroicKatora/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HeroicKatora/subscriptions","organizations_url":"https://api.github.com/users/HeroicKatora/orgs","repos_url":"https://api.github.com/users/HeroicKatora/repos","events_url":"https://api.github.com/users/HeroicKatora/events{/privacy}","received_events_url":"https://api.github.com/users/HeroicKatora/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-10-22T18:10:02Z","updated_at":"2018-10-23T10:15:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3bfb748ca7da0efb52d9b11e28729b7ea03708c0","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/oberien/pundoc/pulls/4/commits","review_comments_url":"https://api.github.com/repos/oberien/pundoc/pulls/4/comments","review_comment_url":"https://api.github.com/repos/oberien/pundoc/pulls/comments{/number}","comments_url":"https://api.github.com/repos/oberien/pundoc/issues/4/comments","statuses_url":"https://api.github.com/repos/oberien/pundoc/statuses/64b7277239f2bfbbfb0d1d2b3fd1b5bff5bbf21a","head":{"label":"oberien:download","ref":"download","sha":"64b7277239f2bfbbfb0d1d2b3fd1b5bff5bbf21a","user":{"login":"oberien","id":4820508,"node_id":"MDQ6VXNlcjQ4MjA1MDg=","avatar_url":"https://avatars1.githubusercontent.com/u/4820508?v=4","gravatar_id":"","url":"https://api.github.com/users/oberien","html_url":"https://github.com/oberien","followers_url":"https://api.github.com/users/oberien/followers","following_url":"https://api.github.com/users/oberien/following{/other_user}","gists_url":"https://api.github.com/users/oberien/gists{/gist_id}","starred_url":"https://api.github.com/users/oberien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oberien/subscriptions","organizations_url":"https://api.github.com/users/oberien/orgs","repos_url":"https://api.github.com/users/oberien/repos","events_url":"https://api.github.com/users/oberien/events{/privacy}","received_events_url":"https://api.github.com/users/oberien/received_events","type":"User","site_admin":false},"repo":{"id":143632602,"node_id":"MDEwOlJlcG9zaXRvcnkxNDM2MzI2MDI=","name":"pundoc","full_name":"oberien/pundoc","private":false,"owner":{"login":"oberien","id":4820508,"node_id":"MDQ6VXNlcjQ4MjA1MDg=","avatar_url":"https://avatars1.githubusercontent.com/u/4820508?v=4","gravatar_id":"","url":"https://api.github.com/users/oberien","html_url":"https://github.com/oberien","followers_url":"https://api.github.com/users/oberien/followers","following_url":"https://api.github.com/users/oberien/following{/other_user}","gists_url":"https://api.github.com/users/oberien/gists{/gist_id}","starred_url":"https://api.github.com/users/oberien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oberien/subscriptions","organizations_url":"https://api.github.com/users/oberien/orgs","repos_url":"https://api.github.com/users/oberien/repos","events_url":"https://api.github.com/users/oberien/events{/privacy}","received_events_url":"https://api.github.com/users/oberien/received_events","type":"User","site_admin":false},"html_url":"https://github.com/oberien/pundoc","description":"Markdown to Latex converter","fork":false,"url":"https://api.github.com/repos/oberien/pundoc","forks_url":"https://api.github.com/repos/oberien/pundoc/forks","keys_url":"https://api.github.com/repos/oberien/pundoc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/oberien/pundoc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/oberien/pundoc/teams","hooks_url":"https://api.github.com/repos/oberien/pundoc/hooks","issue_events_url":"https://api.github.com/repos/oberien/pundoc/issues/events{/number}","events_url":"https://api.github.com/repos/oberien/pundoc/events","assignees_url":"https://api.github.com/repos/oberien/pundoc/assignees{/user}","branches_url":"https://api.github.com/repos/oberien/pundoc/branches{/branch}","tags_url":"https://api.github.com/repos/oberien/pundoc/tags","blobs_url":"https://api.github.com/repos/oberien/pundoc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/oberien/pundoc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/oberien/pundoc/git/refs{/sha}","trees_url":"https://api.github.com/repos/oberien/pundoc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/oberien/pundoc/statuses/{sha}","languages_url":"https://api.github.com/repos/oberien/pundoc/languages","stargazers_url":"https://api.github.com/repos/oberien/pundoc/stargazers","contributors_url":"https://api.github.com/repos/oberien/pundoc/contributors","subscribers_url":"https://api.github.com/repos/oberien/pundoc/subscribers","subscription_url":"https://api.github.com/repos/oberien/pundoc/subscription","commits_url":"https://api.github.com/repos/oberien/pundoc/commits{/sha}","git_commits_url":"https://api.github.com/repos/oberien/pundoc/git/commits{/sha}","comments_url":"https://api.github.com/repos/oberien/pundoc/comments{/number}","issue_comment_url":"https://api.github.com/repos/oberien/pundoc/issues/comments{/number}","contents_url":"https://api.github.com/repos/oberien/pundoc/contents/{+path}","compare_url":"https://api.github.com/repos/oberien/pundoc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/oberien/pundoc/merges","archive_url":"https://api.github.com/repos/oberien/pundoc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/oberien/pundoc/downloads","issues_url":"https://api.github.com/repos/oberien/pundoc/issues{/number}","pulls_url":"https://api.github.com/repos/oberien/pundoc/pulls{/number}","milestones_url":"https://api.github.com/repos/oberien/pundoc/milestones{/number}","notifications_url":"https://api.github.com/repos/oberien/pundoc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/oberien/pundoc/labels{/name}","releases_url":"https://api.github.com/repos/oberien/pundoc/releases{/id}","deployments_url":"https://api.github.com/repos/oberien/pundoc/deployments","created_at":"2018-08-05T17:11:26Z","updated_at":"2018-10-22T19:46:54Z","pushed_at":"2018-10-22T18:10:03Z","git_url":"git://github.com/oberien/pundoc.git","ssh_url":"git@github.com:oberien/pundoc.git","clone_url":"https://github.com/oberien/pundoc.git","svn_url":"https://github.com/oberien/pundoc","homepage":null,"size":191,"stargazers_count":1,"watchers_count":1,"language":"Rust","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"archived":false,"open_issues_count":8,"license":null,"forks":1,"open_issues":8,"watchers":1,"default_branch":"master"}},"base":{"label":"oberien:master","ref":"master","sha":"ea18558e54ae3593938ffd2423db9a8aabb98707","user":{"login":"oberien","id":4820508,"node_id":"MDQ6VXNlcjQ4MjA1MDg=","avatar_url":"https://avatars1.githubusercontent.com/u/4820508?v=4","gravatar_id":"","url":"https://api.github.com/users/oberien","html_url":"https://github.com/oberien","followers_url":"https://api.github.com/users/oberien/followers","following_url":"https://api.github.com/users/oberien/following{/other_user}","gists_url":"https://api.github.com/users/oberien/gists{/gist_id}","starred_url":"https://api.github.com/users/oberien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oberien/subscriptions","organizations_url":"https://api.github.com/users/oberien/orgs","repos_url":"https://api.github.com/users/oberien/repos","events_url":"https://api.github.com/users/oberien/events{/privacy}","received_events_url":"https://api.github.com/users/oberien/received_events","type":"User","site_admin":false},"repo":{"id":143632602,"node_id":"MDEwOlJlcG9zaXRvcnkxNDM2MzI2MDI=","name":"pundoc","full_name":"oberien/pundoc","private":false,"owner":{"login":"oberien","id":4820508,"node_id":"MDQ6VXNlcjQ4MjA1MDg=","avatar_url":"https://avatars1.githubusercontent.com/u/4820508?v=4","gravatar_id":"","url":"https://api.github.com/users/oberien","html_url":"https://github.com/oberien","followers_url":"https://api.github.com/users/oberien/followers","following_url":"https://api.github.com/users/oberien/following{/other_user}","gists_url":"https://api.github.com/users/oberien/gists{/gist_id}","starred_url":"https://api.github.com/users/oberien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oberien/subscriptions","organizations_url":"https://api.github.com/users/oberien/orgs","repos_url":"https://api.github.com/users/oberien/repos","events_url":"https://api.github.com/users/oberien/events{/privacy}","received_events_url":"https://api.github.com/users/oberien/received_events","type":"User","site_admin":false},"html_url":"https://github.com/oberien/pundoc","description":"Markdown to Latex converter","fork":false,"url":"https://api.github.com/repos/oberien/pundoc","forks_url":"https://api.github.com/repos/oberien/pundoc/forks","keys_url":"https://api.github.com/repos/oberien/pundoc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/oberien/pundoc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/oberien/pundoc/teams","hooks_url":"https://api.github.com/repos/oberien/pundoc/hooks","issue_events_url":"https://api.github.com/repos/oberien/pundoc/issues/events{/number}","events_url":"https://api.github.com/repos/oberien/pundoc/events","assignees_url":"https://api.github.com/repos/oberien/pundoc/assignees{/user}","branches_url":"https://api.github.com/repos/oberien/pundoc/branches{/branch}","tags_url":"https://api.github.com/repos/oberien/pundoc/tags","blobs_url":"https://api.github.com/repos/oberien/pundoc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/oberien/pundoc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/oberien/pundoc/git/refs{/sha}","trees_url":"https://api.github.com/repos/oberien/pundoc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/oberien/pundoc/statuses/{sha}","languages_url":"https://api.github.com/repos/oberien/pundoc/languages","stargazers_url":"https://api.github.com/repos/oberien/pundoc/stargazers","contributors_url":"https://api.github.com/repos/oberien/pundoc/contributors","subscribers_url":"https://api.github.com/repos/oberien/pundoc/subscribers","subscription_url":"https://api.github.com/repos/oberien/pundoc/subscription","commits_url":"https://api.github.com/repos/oberien/pundoc/commits{/sha}","git_commits_url":"https://api.github.com/repos/oberien/pundoc/git/commits{/sha}","comments_url":"https://api.github.com/repos/oberien/pundoc/comments{/number}","issue_comment_url":"https://api.github.com/repos/oberien/pundoc/issues/comments{/number}","contents_url":"https://api.github.com/repos/oberien/pundoc/contents/{+path}","compare_url":"https://api.github.com/repos/oberien/pundoc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/oberien/pundoc/merges","archive_url":"https://api.github.com/repos/oberien/pundoc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/oberien/pundoc/downloads","issues_url":"https://api.github.com/repos/oberien/pundoc/issues{/number}","pulls_url":"https://api.github.com/repos/oberien/pundoc/pulls{/number}","milestones_url":"https://api.github.com/repos/oberien/pundoc/milestones{/number}","notifications_url":"https://api.github.com/repos/oberien/pundoc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/oberien/pundoc/labels{/name}","releases_url":"https://api.github.com/repos/oberien/pundoc/releases{/id}","deployments_url":"https://api.github.com/repos/oberien/pundoc/deployments","created_at":"2018-08-05T17:11:26Z","updated_at":"2018-10-22T19:46:54Z","pushed_at":"2018-10-22T18:10:03Z","git_url":"git://github.com/oberien/pundoc.git","ssh_url":"git@github.com:oberien/pundoc.git","clone_url":"https://github.com/oberien/pundoc.git","svn_url":"https://github.com/oberien/pundoc","homepage":null,"size":191,"stargazers_count":1,"watchers_count":1,"language":"Rust","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"archived":false,"open_issues_count":8,"license":null,"forks":1,"open_issues":8,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/oberien/pundoc/pulls/4"},"html":{"href":"https://github.com/oberien/pundoc/pull/4"},"issue":{"href":"https://api.github.com/repos/oberien/pundoc/issues/4"},"comments":{"href":"https://api.github.com/repos/oberien/pundoc/issues/4/comments"},"review_comments":{"href":"https://api.github.com/repos/oberien/pundoc/pulls/4/comments"},"review_comment":{"href":"https://api.github.com/repos/oberien/pundoc/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/oberien/pundoc/pulls/4/commits"},"statuses":{"href":"https://api.github.com/repos/oberien/pundoc/statuses/64b7277239f2bfbbfb0d1d2b3fd1b5bff5bbf21a"}},"author_association":"COLLABORATOR"}}
{"actor":{"display_login":"oberien"}}