{"action":"created","comment":{"url":"https://api.github.com/repos/foodcoops/foodsoft/pulls/comments/62327807","id":62327807,"diff_hunk":"@@ -0,0 +1,63 @@\n+require 'filemagic'\n+\n+class DocumentsController < ApplicationController\n+  before_filter -> { require_plugin_enabled FoodsoftDocuments }\n+\n+  def index\n+    if params[\"sort\"]\n+      sort = case params[\"sort\"]\n+               when \"name\" then \"name\"\n+               when \"created_at\" then \"created_at\"\n+               when \"name_reverse\" then \"name DESC\"\n+               when \"created_at_reverse\" then \"created_at DESC\"\n+             end\n+    else\n+      sort = \"name\"\n+    end\n+\n+    @documents = Document.page(params[:page]).per(@per_page).order(sort)\n+  end\n+\n+  def new\n+    @document = Document.new\n+  end\n+\n+  def create\n+    @document = Document.new\n+    @document.data = params[:document][:data].read\n+    @document.mime = FileMagic.new(FileMagic::MAGIC_MIME).buffer(@document.data)\n+    if params[:document][:name] == ''\n+      name = params[:document][:data].original_filename\n+      name = File.basename(name)\n+      @document.name = name.gsub(/[^\\w\\.\\-]/, '_')\n+    else\n+      @document.name = params[:document][:name]\n+    end\n+    @document.created_by = current_user\n+    @document.save!\n+    redirect_to documents_path, notice: I18n.t('documents.create.notice')\n+  rescue => error\n+    redirect_to documents_path, alert: t('documents.create.error', error: error.message)\n+  end\n+\n+  def destroy\n+    @document = Document.find(params[:id])\n+    if @document.created_by == current_user or current_user.role_admin?\n+      @document.destroy\n+      redirect_to documents_path, notice: t('documents.destroy.notice')\n+    else\n+      redirect_to documents_path, alert: t('documents.destroy.no_right')\n+    end\n+  rescue => error\n+    redirect_to documents_path, alert: t('documents.destroy.error', error: error.message)\n+  end\n+\n+  def show\n+    @document = Document.find(params[:id])\n+    filename = @document.name\n+    unless filename.include? '.'\n+      filename += '.' + MIME::Types[@document.mime].first.preferred_extension\n+    end","path":"plugins/documents/app/controllers/documents_controller.rb","position":60,"original_position":60,"commit_id":"3a800b317011df6d9f0c94bd40d924a1badf2ea2","original_commit_id":"3a800b317011df6d9f0c94bd40d924a1badf2ea2","user":{"login":"paroga","id":337009,"avatar_url":"https://avatars.githubusercontent.com/u/337009?v=3","gravatar_id":"","url":"https://api.github.com/users/paroga","html_url":"https://github.com/paroga","followers_url":"https://api.github.com/users/paroga/followers","following_url":"https://api.github.com/users/paroga/following{/other_user}","gists_url":"https://api.github.com/users/paroga/gists{/gist_id}","starred_url":"https://api.github.com/users/paroga/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paroga/subscriptions","organizations_url":"https://api.github.com/users/paroga/orgs","repos_url":"https://api.github.com/users/paroga/repos","events_url":"https://api.github.com/users/paroga/events{/privacy}","received_events_url":"https://api.github.com/users/paroga/received_events","type":"User","site_admin":false},"body":"Yes, but e.g. a DOC file can contain malicious code too, but it's still a valid filetype I want to be able to store. This has nothing to do with security, it's only a feature to get nice filenames (if not provided by the uploading user)","created_at":"2016-05-06T13:16:24Z","updated_at":"2016-05-06T13:16:24Z","html_url":"https://github.com/foodcoops/foodsoft/pull/353#discussion_r62327807","pull_request_url":"https://api.github.com/repos/foodcoops/foodsoft/pulls/353","_links":{"self":{"href":"https://api.github.com/repos/foodcoops/foodsoft/pulls/comments/62327807"},"html":{"href":"https://github.com/foodcoops/foodsoft/pull/353#discussion_r62327807"},"pull_request":{"href":"https://api.github.com/repos/foodcoops/foodsoft/pulls/353"}}},"pull_request":{"url":"https://api.github.com/repos/foodcoops/foodsoft/pulls/353","id":29537994,"html_url":"https://github.com/foodcoops/foodsoft/pull/353","diff_url":"https://github.com/foodcoops/foodsoft/pull/353.diff","patch_url":"https://github.com/foodcoops/foodsoft/pull/353.patch","issue_url":"https://api.github.com/repos/foodcoops/foodsoft/issues/353","number":353,"state":"open","locked":false,"title":"Add documents","user":{"login":"paroga","id":337009,"avatar_url":"https://avatars.githubusercontent.com/u/337009?v=3","gravatar_id":"","url":"https://api.github.com/users/paroga","html_url":"https://github.com/paroga","followers_url":"https://api.github.com/users/paroga/followers","following_url":"https://api.github.com/users/paroga/following{/other_user}","gists_url":"https://api.github.com/users/paroga/gists{/gist_id}","starred_url":"https://api.github.com/users/paroga/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paroga/subscriptions","organizations_url":"https://api.github.com/users/paroga/orgs","repos_url":"https://api.github.com/users/paroga/repos","events_url":"https://api.github.com/users/paroga/events{/privacy}","received_events_url":"https://api.github.com/users/paroga/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-02-18T16:12:15Z","updated_at":"2016-05-06T13:16:24Z","closed_at":null,"merged_at":null,"merge_commit_sha":"11dc8ccae247a21981666c2a6e81a1d185deb10f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/foodcoops/foodsoft/pulls/353/commits","review_comments_url":"https://api.github.com/repos/foodcoops/foodsoft/pulls/353/comments","review_comment_url":"https://api.github.com/repos/foodcoops/foodsoft/pulls/comments{/number}","comments_url":"https://api.github.com/repos/foodcoops/foodsoft/issues/353/comments","statuses_url":"https://api.github.com/repos/foodcoops/foodsoft/statuses/3a800b317011df6d9f0c94bd40d924a1badf2ea2","head":{"label":"foodcoop1040:documents","ref":"documents","sha":"3a800b317011df6d9f0c94bd40d924a1badf2ea2","user":{"login":"foodcoop1040","id":10269223,"avatar_url":"https://avatars.githubusercontent.com/u/10269223?v=3","gravatar_id":"","url":"https://api.github.com/users/foodcoop1040","html_url":"https://github.com/foodcoop1040","followers_url":"https://api.github.com/users/foodcoop1040/followers","following_url":"https://api.github.com/users/foodcoop1040/following{/other_user}","gists_url":"https://api.github.com/users/foodcoop1040/gists{/gist_id}","starred_url":"https://api.github.com/users/foodcoop1040/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/foodcoop1040/subscriptions","organizations_url":"https://api.github.com/users/foodcoop1040/orgs","repos_url":"https://api.github.com/users/foodcoop1040/repos","events_url":"https://api.github.com/users/foodcoop1040/events{/privacy}","received_events_url":"https://api.github.com/users/foodcoop1040/received_events","type":"Organization","site_admin":false},"repo":{"id":28336053,"name":"foodsoft","full_name":"foodcoop1040/foodsoft","owner":{"login":"foodcoop1040","id":10269223,"avatar_url":"https://avatars.githubusercontent.com/u/10269223?v=3","gravatar_id":"","url":"https://api.github.com/users/foodcoop1040","html_url":"https://github.com/foodcoop1040","followers_url":"https://api.github.com/users/foodcoop1040/followers","following_url":"https://api.github.com/users/foodcoop1040/following{/other_user}","gists_url":"https://api.github.com/users/foodcoop1040/gists{/gist_id}","starred_url":"https://api.github.com/users/foodcoop1040/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/foodcoop1040/subscriptions","organizations_url":"https://api.github.com/users/foodcoop1040/orgs","repos_url":"https://api.github.com/users/foodcoop1040/repos","events_url":"https://api.github.com/users/foodcoop1040/events{/privacy}","received_events_url":"https://api.github.com/users/foodcoop1040/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/foodcoop1040/foodsoft","description":"Web-based software to manage a non-profit food coop (product catalog, ordering, accounting, job scheduling).","fork":true,"url":"https://api.github.com/repos/foodcoop1040/foodsoft","forks_url":"https://api.github.com/repos/foodcoop1040/foodsoft/forks","keys_url":"https://api.github.com/repos/foodcoop1040/foodsoft/keys{/key_id}","collaborators_url":"https://api.github.com/repos/foodcoop1040/foodsoft/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/foodcoop1040/foodsoft/teams","hooks_url":"https://api.github.com/repos/foodcoop1040/foodsoft/hooks","issue_events_url":"https://api.github.com/repos/foodcoop1040/foodsoft/issues/events{/number}","events_url":"https://api.github.com/repos/foodcoop1040/foodsoft/events","assignees_url":"https://api.github.com/repos/foodcoop1040/foodsoft/assignees{/user}","branches_url":"https://api.github.com/repos/foodcoop1040/foodsoft/branches{/branch}","tags_url":"https://api.github.com/repos/foodcoop1040/foodsoft/tags","blobs_url":"https://api.github.com/repos/foodcoop1040/foodsoft/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/foodcoop1040/foodsoft/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/foodcoop1040/foodsoft/git/refs{/sha}","trees_url":"https://api.github.com/repos/foodcoop1040/foodsoft/git/trees{/sha}","statuses_url":"https://api.github.com/repos/foodcoop1040/foodsoft/statuses/{sha}","languages_url":"https://api.github.com/repos/foodcoop1040/foodsoft/languages","stargazers_url":"https://api.github.com/repos/foodcoop1040/foodsoft/stargazers","contributors_url":"https://api.github.com/repos/foodcoop1040/foodsoft/contributors","subscribers_url":"https://api.github.com/repos/foodcoop1040/foodsoft/subscribers","subscription_url":"https://api.github.com/repos/foodcoop1040/foodsoft/subscription","commits_url":"https://api.github.com/repos/foodcoop1040/foodsoft/commits{/sha}","git_commits_url":"https://api.github.com/repos/foodcoop1040/foodsoft/git/commits{/sha}","comments_url":"https://api.github.com/repos/foodcoop1040/foodsoft/comments{/number}","issue_comment_url":"https://api.github.com/repos/foodcoop1040/foodsoft/issues/comments{/number}","contents_url":"https://api.github.com/repos/foodcoop1040/foodsoft/contents/{+path}","compare_url":"https://api.github.com/repos/foodcoop1040/foodsoft/compare/{base}...{head}","merges_url":"https://api.github.com/repos/foodcoop1040/foodsoft/merges","archive_url":"https://api.github.com/repos/foodcoop1040/foodsoft/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/foodcoop1040/foodsoft/downloads","issues_url":"https://api.github.com/repos/foodcoop1040/foodsoft/issues{/number}","pulls_url":"https://api.github.com/repos/foodcoop1040/foodsoft/pulls{/number}","milestones_url":"https://api.github.com/repos/foodcoop1040/foodsoft/milestones{/number}","notifications_url":"https://api.github.com/repos/foodcoop1040/foodsoft/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/foodcoop1040/foodsoft/labels{/name}","releases_url":"https://api.github.com/repos/foodcoop1040/foodsoft/releases{/id}","deployments_url":"https://api.github.com/repos/foodcoop1040/foodsoft/deployments","created_at":"2014-12-22T11:31:56Z","updated_at":"2016-02-25T20:25:15Z","pushed_at":"2016-05-06T13:05:35Z","git_url":"git://github.com/foodcoop1040/foodsoft.git","ssh_url":"git@github.com:foodcoop1040/foodsoft.git","clone_url":"https://github.com/foodcoop1040/foodsoft.git","svn_url":"https://github.com/foodcoop1040/foodsoft","homepage":"http://www.foodcoops.net/","size":8468,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"foodcoops:master","ref":"master","sha":"de66cd5a2b48495230296a7052abc7e046834b06","user":{"login":"foodcoops","id":4523295,"avatar_url":"https://avatars.githubusercontent.com/u/4523295?v=3","gravatar_id":"","url":"https://api.github.com/users/foodcoops","html_url":"https://github.com/foodcoops","followers_url":"https://api.github.com/users/foodcoops/followers","following_url":"https://api.github.com/users/foodcoops/following{/other_user}","gists_url":"https://api.github.com/users/foodcoops/gists{/gist_id}","starred_url":"https://api.github.com/users/foodcoops/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/foodcoops/subscriptions","organizations_url":"https://api.github.com/users/foodcoops/orgs","repos_url":"https://api.github.com/users/foodcoops/repos","events_url":"https://api.github.com/users/foodcoops/events{/privacy}","received_events_url":"https://api.github.com/users/foodcoops/received_events","type":"Organization","site_admin":false},"repo":{"id":102952,"name":"foodsoft","full_name":"foodcoops/foodsoft","owner":{"login":"foodcoops","id":4523295,"avatar_url":"https://avatars.githubusercontent.com/u/4523295?v=3","gravatar_id":"","url":"https://api.github.com/users/foodcoops","html_url":"https://github.com/foodcoops","followers_url":"https://api.github.com/users/foodcoops/followers","following_url":"https://api.github.com/users/foodcoops/following{/other_user}","gists_url":"https://api.github.com/users/foodcoops/gists{/gist_id}","starred_url":"https://api.github.com/users/foodcoops/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/foodcoops/subscriptions","organizations_url":"https://api.github.com/users/foodcoops/orgs","repos_url":"https://api.github.com/users/foodcoops/repos","events_url":"https://api.github.com/users/foodcoops/events{/privacy}","received_events_url":"https://api.github.com/users/foodcoops/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/foodcoops/foodsoft","description":"Web-based software to manage a non-profit food coop (product catalog, ordering, accounting, job scheduling).","fork":false,"url":"https://api.github.com/repos/foodcoops/foodsoft","forks_url":"https://api.github.com/repos/foodcoops/foodsoft/forks","keys_url":"https://api.github.com/repos/foodcoops/foodsoft/keys{/key_id}","collaborators_url":"https://api.github.com/repos/foodcoops/foodsoft/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/foodcoops/foodsoft/teams","hooks_url":"https://api.github.com/repos/foodcoops/foodsoft/hooks","issue_events_url":"https://api.github.com/repos/foodcoops/foodsoft/issues/events{/number}","events_url":"https://api.github.com/repos/foodcoops/foodsoft/events","assignees_url":"https://api.github.com/repos/foodcoops/foodsoft/assignees{/user}","branches_url":"https://api.github.com/repos/foodcoops/foodsoft/branches{/branch}","tags_url":"https://api.github.com/repos/foodcoops/foodsoft/tags","blobs_url":"https://api.github.com/repos/foodcoops/foodsoft/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/foodcoops/foodsoft/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/foodcoops/foodsoft/git/refs{/sha}","trees_url":"https://api.github.com/repos/foodcoops/foodsoft/git/trees{/sha}","statuses_url":"https://api.github.com/repos/foodcoops/foodsoft/statuses/{sha}","languages_url":"https://api.github.com/repos/foodcoops/foodsoft/languages","stargazers_url":"https://api.github.com/repos/foodcoops/foodsoft/stargazers","contributors_url":"https://api.github.com/repos/foodcoops/foodsoft/contributors","subscribers_url":"https://api.github.com/repos/foodcoops/foodsoft/subscribers","subscription_url":"https://api.github.com/repos/foodcoops/foodsoft/subscription","commits_url":"https://api.github.com/repos/foodcoops/foodsoft/commits{/sha}","git_commits_url":"https://api.github.com/repos/foodcoops/foodsoft/git/commits{/sha}","comments_url":"https://api.github.com/repos/foodcoops/foodsoft/comments{/number}","issue_comment_url":"https://api.github.com/repos/foodcoops/foodsoft/issues/comments{/number}","contents_url":"https://api.github.com/repos/foodcoops/foodsoft/contents/{+path}","compare_url":"https://api.github.com/repos/foodcoops/foodsoft/compare/{base}...{head}","merges_url":"https://api.github.com/repos/foodcoops/foodsoft/merges","archive_url":"https://api.github.com/repos/foodcoops/foodsoft/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/foodcoops/foodsoft/downloads","issues_url":"https://api.github.com/repos/foodcoops/foodsoft/issues{/number}","pulls_url":"https://api.github.com/repos/foodcoops/foodsoft/pulls{/number}","milestones_url":"https://api.github.com/repos/foodcoops/foodsoft/milestones{/number}","notifications_url":"https://api.github.com/repos/foodcoops/foodsoft/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/foodcoops/foodsoft/labels{/name}","releases_url":"https://api.github.com/repos/foodcoops/foodsoft/releases{/id}","deployments_url":"https://api.github.com/repos/foodcoops/foodsoft/deployments","created_at":"2009-01-07T22:38:49Z","updated_at":"2016-04-29T23:23:13Z","pushed_at":"2016-05-06T13:05:36Z","git_url":"git://github.com/foodcoops/foodsoft.git","ssh_url":"git@github.com:foodcoops/foodsoft.git","clone_url":"https://github.com/foodcoops/foodsoft.git","svn_url":"https://github.com/foodcoops/foodsoft","homepage":"http://www.foodcoops.net/","size":9530,"stargazers_count":102,"watchers_count":102,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":67,"mirror_url":null,"open_issues_count":67,"forks":67,"open_issues":67,"watchers":102,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/foodcoops/foodsoft/pulls/353"},"html":{"href":"https://github.com/foodcoops/foodsoft/pull/353"},"issue":{"href":"https://api.github.com/repos/foodcoops/foodsoft/issues/353"},"comments":{"href":"https://api.github.com/repos/foodcoops/foodsoft/issues/353/comments"},"review_comments":{"href":"https://api.github.com/repos/foodcoops/foodsoft/pulls/353/comments"},"review_comment":{"href":"https://api.github.com/repos/foodcoops/foodsoft/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/foodcoops/foodsoft/pulls/353/commits"},"statuses":{"href":"https://api.github.com/repos/foodcoops/foodsoft/statuses/3a800b317011df6d9f0c94bd40d924a1badf2ea2"}}}}