{"action":"created","comment":{"url":"https://api.github.com/repos/inventid/live-image-resize/pulls/comments/69107238","id":69107238,"diff_hunk":"@@ -1,363 +1,137 @@\n import express from \"express\";\n-import gm from \"gm\";\n-import formidable from \"formidable\";\n-import fs from \"fs-extra\";\n import config from \"config\";\n-import responseTime from \"response-time\";\n import bodyParser from \"body-parser\";\n-import pg from \"pg\";\n import migrateAndStart from \"pg-migration\";\n-import uuid from \"uuid\";\n-import Token from \"./Token\";\n-import log from \"./Log\";\n-import helpers from \"./Helpers\";\n-import parsing from \"./UrlParsing\";\n-import S3 from \"./AwsS3.js\";\n-import requestLogger from \"./RequestLogger.js\";\n-\n-const im = gm.subClass({imageMagick: true});\n+import pg from \"pg\";\n+import formidable from \"formidable\";\n+import log from \"./log\";\n+import urlParameters from \"./urlParameters\";\n+import imageResponse from \"./imageResponse\";\n+import token from \"./token\";\n+import {isNotUndefined} from \"./helper\";\n \n-const connectionString = `postgres://${config.get('postgresql.user')}:${config.get('postgresql.password')}@${config.get('postgresql.host')}/${config.get('postgresql.database')}`; //eslint-disable-line max-len\n-let token;\n let db;\n+const connectionString = `postgres://${config.get('postgresql.user')}:${config.get('postgresql.password')}@${config.get('postgresql.host')}/${config.get('postgresql.database')}`; //eslint-disable-line max-len\n \n-// Re-use existing prepared queries\n-const insertImage = 'INSERT INTO images (id, x, y, fit, file_type, url) VALUES ($1,$2,$3,$4,$5,$6)';\n-const selectImage = 'SELECT url FROM images WHERE id=$1 AND x=$2 AND y=$3 AND fit=$4 AND file_type=$5';\n-\n-// This method creates the URL key from the parameters of the image\n-function getKeyFromParams(params) {\n-  return `${params.fileName}_${params.resolutionX}x${params.resolutionY}.${params.fit}.${params.fileType}`;\n-}\n-\n-// This methods handles the correct resizing of an image\n-function correctlyResize(file, params, callback) {\n-  let workImageClient = im(file);\n-  //Interlacing for png isn't efficient (both in filesize as render performance), so we only do it for jpg\n-  if (params.fileType === 'jpg') {\n-    workImageClient = workImageClient.interlace('Line');\n-  }\n-\n-  // Determine how to crop this\n-  if (params.fit === 'crop') {\n-    workImageClient = workImageClient.resize(params.resolutionX, params.resolutionY, '^');\n-    const tmpFile = `/tmp/${uuid.v4()}`;\n-    workImageClient.write(tmpFile, (err) => {\n-      if (err) {\n-        log.log('error', `Could not write tempFile: ${err}`);\n-        return;\n-      }\n-      workImageClient = im(tmpFile);\n-      workImageClient.size((err, newSize) => {\n-        if (err) {\n-          log.log('error', `Could not resize tempFile: ${err}`);\n-          return;\n-        }\n-        workImageClient = workImageClient\n-          .repage(newSize.width, newSize.height, 0, 0)\n-          .gravity('Center')\n-          .crop(params.resolutionX, params.resolutionY, '!');\n-        callback(workImageClient);\n-      });\n-    });\n-  } else {\n-    workImageClient = workImageClient.resize(params.resolutionX, params.resolutionY);\n-    if (params.fit === 'canvas') {\n-      workImageClient = workImageClient.gravity('Center').extent(params.resolutionX, params.resolutionY);\n-    }\n-    callback(workImageClient);\n-  }\n-}\n-\n-const Image = {\n-  get(req, res) {\n-    if (!parsing.isValidRequest(req.url)) {\n-      // Invalid URL\n-      helpers.send404(res, req.url);\n-      return;\n-    }\n-\n-    const params = parsing.getImageParams(req);\n-\n-    if (parsing.supportedFileType(params.fileType) === null) {\n-      helpers.send415(res, params.fileType);\n-      return;\n-    }\n-\n-    let valid = true;\n-\n-    // Check if we are allowed to serve an image of this size and optionally redirect\n-    const providedRatio = params.resolutionX / params.resolutionY;\n-    if (params.resolutionX > config.get('constraints.max_width')) {\n-      params.resolutionX = config.get('constraints.max_width');\n-      if (params.fit === 'crop') {\n-        params.resolutionY = params.resolutionX / providedRatio;\n-      }\n-      valid = false;\n-    }\n-    if (params.resolutionY > config.get('constraints.max_height')) {\n-      params.resolutionY = config.get('constraints.max_height');\n-      if (params.fit === 'crop') {\n-        params.resolutionX = params.resolutionY * providedRatio;\n-      }\n-      valid = false;\n-    }\n-    if (!valid) {\n-      helpers.send307DueTooLarge(res, params);\n-      return;\n-    }\n-\n-    //HEAD requests won't be redirected automatically, so instead we'll always either return a 200\n-    //or a 404, indicating if the corresponding GET method will result in an image.\n-    if (req.method === 'HEAD') {\n-      Image.canServeFile(params, (canServe) => {\n-        if (canServe) {\n-          res.status(200).end();\n-        } else {\n-          res.status(404).end();\n-        }\n-      });\n-      return;\n-    }\n-    log.log('info', `Requesting file ${params.fileName} in ${params.fileType} format in a ${params.resolutionX}x${params.resolutionY}px resolution`); //eslint-disable-line max-len\n-\n-    Image.checkCacheOrCreate(params, res);\n-  },\n-  canServeFile(params, cb) {\n-    const file = `${config.get('originals_dir')}/${params.fileName}`;\n-    fs.access(file, fs.R_OK, (err) => {\n-      if (!err) {\n-        cb(true);\n-      } else {\n-        cb(false);\n-      }\n-    });\n-  },\n-  checkCacheOrCreate(params, res) {\n-    // Check if it exists in the cache\n-    db.query(selectImage, [params.fileName,\n-      params.resolutionX,\n-      params.resolutionY,\n-      params.fit,\n-      parsing.supportedFileType(params.fileType)\n-    ], (err, data) => {\n-      if (!err && data.rowCount === 1) {\n-        // It is in the cache, so redirect to there\n-        helpers.send307DueToCache(res, params, data.rows[0].url);\n-        return;\n-      }\n-\n-      // It does not exist in the cache, so generate and upload\n-      Image.encodeAndUpload(params, res);\n-    });\n-  },\n-  encodeAndUpload(params, res) {\n-    const file = `${config.get('originals_dir')}/${params.fileName}`;\n-    fs.access(file, (err) => {\n-      if (err) {\n-        log.log('warn', `File ${params.fileName} was requested but did not exist`);\n-        helpers.send404(res, params.fileName);\n-        return;\n-      }\n-\n-      // Get the image and resize it\n-      res.writeHead(200, {\n-        'Content-Type': parsing.supportedFileType(params.fileType),\n-        Expires: helpers.farFutureDate(),\n-        'Cache-Control': 'public'\n-      });\n+process.on('uncaughtException', function (err) {\n+  console.log(err);","path":"src/index.js","position":178,"original_position":178,"commit_id":"0a6b236c5165b7f7e3b967459e084bac437c874e","original_commit_id":"0a6b236c5165b7f7e3b967459e084bac437c874e","user":{"login":"TiddoLangerak","id":1759192,"avatar_url":"https://avatars.githubusercontent.com/u/1759192?v=3","gravatar_id":"","url":"https://api.github.com/users/TiddoLangerak","html_url":"https://github.com/TiddoLangerak","followers_url":"https://api.github.com/users/TiddoLangerak/followers","following_url":"https://api.github.com/users/TiddoLangerak/following{/other_user}","gists_url":"https://api.github.com/users/TiddoLangerak/gists{/gist_id}","starred_url":"https://api.github.com/users/TiddoLangerak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TiddoLangerak/subscriptions","organizations_url":"https://api.github.com/users/TiddoLangerak/orgs","repos_url":"https://api.github.com/users/TiddoLangerak/repos","events_url":"https://api.github.com/users/TiddoLangerak/events{/privacy}","received_events_url":"https://api.github.com/users/TiddoLangerak/received_events","type":"User","site_admin":false},"body":"You're missing the `await gracefullySuicide(); process.exit(1);`","created_at":"2016-06-30T10:14:35Z","updated_at":"2016-06-30T10:14:35Z","html_url":"https://github.com/inventid/live-image-resize/pull/70#discussion_r69107238","pull_request_url":"https://api.github.com/repos/inventid/live-image-resize/pulls/70","_links":{"self":{"href":"https://api.github.com/repos/inventid/live-image-resize/pulls/comments/69107238"},"html":{"href":"https://github.com/inventid/live-image-resize/pull/70#discussion_r69107238"},"pull_request":{"href":"https://api.github.com/repos/inventid/live-image-resize/pulls/70"}}},"pull_request":{"url":"https://api.github.com/repos/inventid/live-image-resize/pulls/70","id":75534853,"html_url":"https://github.com/inventid/live-image-resize/pull/70","diff_url":"https://github.com/inventid/live-image-resize/pull/70.diff","patch_url":"https://github.com/inventid/live-image-resize/pull/70.patch","issue_url":"https://api.github.com/repos/inventid/live-image-resize/issues/70","number":70,"state":"open","locked":false,"title":"Rewrite to proper logic","user":{"login":"rogierslag","id":2778689,"avatar_url":"https://avatars.githubusercontent.com/u/2778689?v=3","gravatar_id":"","url":"https://api.github.com/users/rogierslag","html_url":"https://github.com/rogierslag","followers_url":"https://api.github.com/users/rogierslag/followers","following_url":"https://api.github.com/users/rogierslag/following{/other_user}","gists_url":"https://api.github.com/users/rogierslag/gists{/gist_id}","starred_url":"https://api.github.com/users/rogierslag/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rogierslag/subscriptions","organizations_url":"https://api.github.com/users/rogierslag/orgs","repos_url":"https://api.github.com/users/rogierslag/repos","events_url":"https://api.github.com/users/rogierslag/events{/privacy}","received_events_url":"https://api.github.com/users/rogierslag/received_events","type":"User","site_admin":false},"body":"@TiddoLangerak @wouter-willems @joostverdoorn Can I haz a first 👀 ? Did a small Tiddo and wanted to rewrite this to proper logic. This way the code does not hurt my eyes anymore.\r\n\r\n- Use async await instead of callback hell\r\n- Proper modularization\r\n- Proper passthrough of information\r\n- Much better imagemagick logic\r\n- Cropping on upload works\r\n\r\nThe diff is terrible though, so you might be better off checking the branch itself.","created_at":"2016-06-28T19:22:07Z","updated_at":"2016-06-30T10:14:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0b26a24fc741c367c11ef89e9061433a64e166c1","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/inventid/live-image-resize/pulls/70/commits","review_comments_url":"https://api.github.com/repos/inventid/live-image-resize/pulls/70/comments","review_comment_url":"https://api.github.com/repos/inventid/live-image-resize/pulls/comments{/number}","comments_url":"https://api.github.com/repos/inventid/live-image-resize/issues/70/comments","statuses_url":"https://api.github.com/repos/inventid/live-image-resize/statuses/0a6b236c5165b7f7e3b967459e084bac437c874e","head":{"label":"inventid:rewrite","ref":"rewrite","sha":"0a6b236c5165b7f7e3b967459e084bac437c874e","user":{"login":"inventid","id":4517861,"avatar_url":"https://avatars.githubusercontent.com/u/4517861?v=3","gravatar_id":"","url":"https://api.github.com/users/inventid","html_url":"https://github.com/inventid","followers_url":"https://api.github.com/users/inventid/followers","following_url":"https://api.github.com/users/inventid/following{/other_user}","gists_url":"https://api.github.com/users/inventid/gists{/gist_id}","starred_url":"https://api.github.com/users/inventid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inventid/subscriptions","organizations_url":"https://api.github.com/users/inventid/orgs","repos_url":"https://api.github.com/users/inventid/repos","events_url":"https://api.github.com/users/inventid/events{/privacy}","received_events_url":"https://api.github.com/users/inventid/received_events","type":"Organization","site_admin":false},"repo":{"id":31164435,"name":"live-image-resize","full_name":"inventid/live-image-resize","owner":{"login":"inventid","id":4517861,"avatar_url":"https://avatars.githubusercontent.com/u/4517861?v=3","gravatar_id":"","url":"https://api.github.com/users/inventid","html_url":"https://github.com/inventid","followers_url":"https://api.github.com/users/inventid/followers","following_url":"https://api.github.com/users/inventid/following{/other_user}","gists_url":"https://api.github.com/users/inventid/gists{/gist_id}","starred_url":"https://api.github.com/users/inventid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inventid/subscriptions","organizations_url":"https://api.github.com/users/inventid/orgs","repos_url":"https://api.github.com/users/inventid/repos","events_url":"https://api.github.com/users/inventid/events{/privacy}","received_events_url":"https://api.github.com/users/inventid/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/inventid/live-image-resize","description":"Resize images on the fly and store pre-computed results on AWS S3","fork":false,"url":"https://api.github.com/repos/inventid/live-image-resize","forks_url":"https://api.github.com/repos/inventid/live-image-resize/forks","keys_url":"https://api.github.com/repos/inventid/live-image-resize/keys{/key_id}","collaborators_url":"https://api.github.com/repos/inventid/live-image-resize/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/inventid/live-image-resize/teams","hooks_url":"https://api.github.com/repos/inventid/live-image-resize/hooks","issue_events_url":"https://api.github.com/repos/inventid/live-image-resize/issues/events{/number}","events_url":"https://api.github.com/repos/inventid/live-image-resize/events","assignees_url":"https://api.github.com/repos/inventid/live-image-resize/assignees{/user}","branches_url":"https://api.github.com/repos/inventid/live-image-resize/branches{/branch}","tags_url":"https://api.github.com/repos/inventid/live-image-resize/tags","blobs_url":"https://api.github.com/repos/inventid/live-image-resize/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/inventid/live-image-resize/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/inventid/live-image-resize/git/refs{/sha}","trees_url":"https://api.github.com/repos/inventid/live-image-resize/git/trees{/sha}","statuses_url":"https://api.github.com/repos/inventid/live-image-resize/statuses/{sha}","languages_url":"https://api.github.com/repos/inventid/live-image-resize/languages","stargazers_url":"https://api.github.com/repos/inventid/live-image-resize/stargazers","contributors_url":"https://api.github.com/repos/inventid/live-image-resize/contributors","subscribers_url":"https://api.github.com/repos/inventid/live-image-resize/subscribers","subscription_url":"https://api.github.com/repos/inventid/live-image-resize/subscription","commits_url":"https://api.github.com/repos/inventid/live-image-resize/commits{/sha}","git_commits_url":"https://api.github.com/repos/inventid/live-image-resize/git/commits{/sha}","comments_url":"https://api.github.com/repos/inventid/live-image-resize/comments{/number}","issue_comment_url":"https://api.github.com/repos/inventid/live-image-resize/issues/comments{/number}","contents_url":"https://api.github.com/repos/inventid/live-image-resize/contents/{+path}","compare_url":"https://api.github.com/repos/inventid/live-image-resize/compare/{base}...{head}","merges_url":"https://api.github.com/repos/inventid/live-image-resize/merges","archive_url":"https://api.github.com/repos/inventid/live-image-resize/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/inventid/live-image-resize/downloads","issues_url":"https://api.github.com/repos/inventid/live-image-resize/issues{/number}","pulls_url":"https://api.github.com/repos/inventid/live-image-resize/pulls{/number}","milestones_url":"https://api.github.com/repos/inventid/live-image-resize/milestones{/number}","notifications_url":"https://api.github.com/repos/inventid/live-image-resize/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/inventid/live-image-resize/labels{/name}","releases_url":"https://api.github.com/repos/inventid/live-image-resize/releases{/id}","deployments_url":"https://api.github.com/repos/inventid/live-image-resize/deployments","created_at":"2015-02-22T13:52:16Z","updated_at":"2016-06-28T22:01:20Z","pushed_at":"2016-06-29T18:32:13Z","git_url":"git://github.com/inventid/live-image-resize.git","ssh_url":"git@github.com:inventid/live-image-resize.git","clone_url":"https://github.com/inventid/live-image-resize.git","svn_url":"https://github.com/inventid/live-image-resize","homepage":null,"size":276,"stargazers_count":6,"watchers_count":6,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":6,"forks":2,"open_issues":6,"watchers":6,"default_branch":"develop"}},"base":{"label":"inventid:develop","ref":"develop","sha":"a75d5a375c28edc7109577ee1a66f504dfdfae4d","user":{"login":"inventid","id":4517861,"avatar_url":"https://avatars.githubusercontent.com/u/4517861?v=3","gravatar_id":"","url":"https://api.github.com/users/inventid","html_url":"https://github.com/inventid","followers_url":"https://api.github.com/users/inventid/followers","following_url":"https://api.github.com/users/inventid/following{/other_user}","gists_url":"https://api.github.com/users/inventid/gists{/gist_id}","starred_url":"https://api.github.com/users/inventid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inventid/subscriptions","organizations_url":"https://api.github.com/users/inventid/orgs","repos_url":"https://api.github.com/users/inventid/repos","events_url":"https://api.github.com/users/inventid/events{/privacy}","received_events_url":"https://api.github.com/users/inventid/received_events","type":"Organization","site_admin":false},"repo":{"id":31164435,"name":"live-image-resize","full_name":"inventid/live-image-resize","owner":{"login":"inventid","id":4517861,"avatar_url":"https://avatars.githubusercontent.com/u/4517861?v=3","gravatar_id":"","url":"https://api.github.com/users/inventid","html_url":"https://github.com/inventid","followers_url":"https://api.github.com/users/inventid/followers","following_url":"https://api.github.com/users/inventid/following{/other_user}","gists_url":"https://api.github.com/users/inventid/gists{/gist_id}","starred_url":"https://api.github.com/users/inventid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/inventid/subscriptions","organizations_url":"https://api.github.com/users/inventid/orgs","repos_url":"https://api.github.com/users/inventid/repos","events_url":"https://api.github.com/users/inventid/events{/privacy}","received_events_url":"https://api.github.com/users/inventid/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/inventid/live-image-resize","description":"Resize images on the fly and store pre-computed results on AWS S3","fork":false,"url":"https://api.github.com/repos/inventid/live-image-resize","forks_url":"https://api.github.com/repos/inventid/live-image-resize/forks","keys_url":"https://api.github.com/repos/inventid/live-image-resize/keys{/key_id}","collaborators_url":"https://api.github.com/repos/inventid/live-image-resize/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/inventid/live-image-resize/teams","hooks_url":"https://api.github.com/repos/inventid/live-image-resize/hooks","issue_events_url":"https://api.github.com/repos/inventid/live-image-resize/issues/events{/number}","events_url":"https://api.github.com/repos/inventid/live-image-resize/events","assignees_url":"https://api.github.com/repos/inventid/live-image-resize/assignees{/user}","branches_url":"https://api.github.com/repos/inventid/live-image-resize/branches{/branch}","tags_url":"https://api.github.com/repos/inventid/live-image-resize/tags","blobs_url":"https://api.github.com/repos/inventid/live-image-resize/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/inventid/live-image-resize/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/inventid/live-image-resize/git/refs{/sha}","trees_url":"https://api.github.com/repos/inventid/live-image-resize/git/trees{/sha}","statuses_url":"https://api.github.com/repos/inventid/live-image-resize/statuses/{sha}","languages_url":"https://api.github.com/repos/inventid/live-image-resize/languages","stargazers_url":"https://api.github.com/repos/inventid/live-image-resize/stargazers","contributors_url":"https://api.github.com/repos/inventid/live-image-resize/contributors","subscribers_url":"https://api.github.com/repos/inventid/live-image-resize/subscribers","subscription_url":"https://api.github.com/repos/inventid/live-image-resize/subscription","commits_url":"https://api.github.com/repos/inventid/live-image-resize/commits{/sha}","git_commits_url":"https://api.github.com/repos/inventid/live-image-resize/git/commits{/sha}","comments_url":"https://api.github.com/repos/inventid/live-image-resize/comments{/number}","issue_comment_url":"https://api.github.com/repos/inventid/live-image-resize/issues/comments{/number}","contents_url":"https://api.github.com/repos/inventid/live-image-resize/contents/{+path}","compare_url":"https://api.github.com/repos/inventid/live-image-resize/compare/{base}...{head}","merges_url":"https://api.github.com/repos/inventid/live-image-resize/merges","archive_url":"https://api.github.com/repos/inventid/live-image-resize/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/inventid/live-image-resize/downloads","issues_url":"https://api.github.com/repos/inventid/live-image-resize/issues{/number}","pulls_url":"https://api.github.com/repos/inventid/live-image-resize/pulls{/number}","milestones_url":"https://api.github.com/repos/inventid/live-image-resize/milestones{/number}","notifications_url":"https://api.github.com/repos/inventid/live-image-resize/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/inventid/live-image-resize/labels{/name}","releases_url":"https://api.github.com/repos/inventid/live-image-resize/releases{/id}","deployments_url":"https://api.github.com/repos/inventid/live-image-resize/deployments","created_at":"2015-02-22T13:52:16Z","updated_at":"2016-06-28T22:01:20Z","pushed_at":"2016-06-29T18:32:13Z","git_url":"git://github.com/inventid/live-image-resize.git","ssh_url":"git@github.com:inventid/live-image-resize.git","clone_url":"https://github.com/inventid/live-image-resize.git","svn_url":"https://github.com/inventid/live-image-resize","homepage":null,"size":276,"stargazers_count":6,"watchers_count":6,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":6,"forks":2,"open_issues":6,"watchers":6,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/inventid/live-image-resize/pulls/70"},"html":{"href":"https://github.com/inventid/live-image-resize/pull/70"},"issue":{"href":"https://api.github.com/repos/inventid/live-image-resize/issues/70"},"comments":{"href":"https://api.github.com/repos/inventid/live-image-resize/issues/70/comments"},"review_comments":{"href":"https://api.github.com/repos/inventid/live-image-resize/pulls/70/comments"},"review_comment":{"href":"https://api.github.com/repos/inventid/live-image-resize/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/inventid/live-image-resize/pulls/70/commits"},"statuses":{"href":"https://api.github.com/repos/inventid/live-image-resize/statuses/0a6b236c5165b7f7e3b967459e084bac437c874e"}}}}
{"actor":{"display_login":"TiddoLangerak"}}