{"action":"created","comment":{"url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/comments/204986689","pull_request_review_id":140165228,"id":204986689,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwNDk4NjY4OQ==","diff_hunk":"@@ -1,6 +1,56 @@\n+from flask import render_template, redirect, url_for, flash\r\n+from forms.upload_form import CheckFile\r\n+import os\r\n+\r\n+app = None\r\n+upload_dir = None  # 文件上传/下载路径\r\n+filelist = None\r\n+\r\n+ALLOWED_EXTENSIONS = ['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'rtf', 'txt',\r\n+                      'pdf', 'png', 'bmp', 'jpg']  # 允许上传的文件格式\r\n+ALLOWED_FILE_SIZE = 10 * 1024 * 1024  # 允许上传的文件大小MB\r\n+\r\n+\r\n+def init(_app):\r\n+    global app\r\n+    global upload_dir\r\n+    app = _app\r\n+    upload_dir = os.path.join(\r\n+        app.instance_path, 'upload'\r\n+    )  # 将文件上传到项目的upload文件夹下\r\n+    if not os.path.exists(upload_dir):\r\n+        os.makedirs(upload_dir)\r\n+        # upload_dir = r'D:\\Users\\蛮小白\\Desktop\\test\\upload'\r\n+\r\n+\r\n+def allowed_file(filename):  # 检查文件格式是否合法\r\n+    return '.' in filename and \\\r\n+           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS\r\n+\r\n+\r\n+def allowed_size(size):  # 检查文件大小是否合法\r\n+    return size <= ALLOWED_FILE_SIZE\r\n+\r\n+\r\n def upload_file():\r\n-    pass\r\n+    form = CheckFile()\r\n+    if form.validate_on_submit():\r\n+        f = form.image.data\r\n+        size = len(f.read())\r\n+        if allowed_file(f.filename):\r\n+            if allowed_size(size):\r\n+                f.seek(0)\r\n+                f.save(os.path.join(upload_dir, f.filename))\r\n+                flash('Upload success。')\r\n+                return redirect(url_for('upload'))\r\n+            else:\r\n+                flash('上传失败，文件大小：<=10M')\r\n+        else:\r\n+            flash('上传失败，允许上传的文件类型：office文档、常见图片类型')\r\n+    return render_template('./upload/upload.html', form=form)\r\n \r\n \r\n-def file_list():\r\n-    pass\r\n+def file_list():  # 文件下载\r\n+    for parent, dirname, filenames in os.walk(upload_dir):\r\n+        filelist = filenames\r\n+    return filelist\r","path":"file.py","position":59,"original_position":59,"commit_id":"6a2977830e14dba526d01a4f77b3e9c01f8b2d34","original_commit_id":"6a2977830e14dba526d01a4f77b3e9c01f8b2d34","user":{"login":"TheMasterOfMagic","id":25054321,"node_id":"MDQ6VXNlcjI1MDU0MzIx","avatar_url":"https://avatars2.githubusercontent.com/u/25054321?v=4","gravatar_id":"","url":"https://api.github.com/users/TheMasterOfMagic","html_url":"https://github.com/TheMasterOfMagic","followers_url":"https://api.github.com/users/TheMasterOfMagic/followers","following_url":"https://api.github.com/users/TheMasterOfMagic/following{/other_user}","gists_url":"https://api.github.com/users/TheMasterOfMagic/gists{/gist_id}","starred_url":"https://api.github.com/users/TheMasterOfMagic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheMasterOfMagic/subscriptions","organizations_url":"https://api.github.com/users/TheMasterOfMagic/orgs","repos_url":"https://api.github.com/users/TheMasterOfMagic/repos","events_url":"https://api.github.com/users/TheMasterOfMagic/events{/privacy}","received_events_url":"https://api.github.com/users/TheMasterOfMagic/received_events","type":"User","site_admin":false},"body":"如果上传目录中有子目录，那么最后返回的`filelist`的值将是最后一个子目录的文件列表。这里显然是明确希望获取上传目录本身的文件列表的（不论是否有子目录），所以直接`filelist = next(os.walk(upload_dir))[-1]`就可以了，没必要用上多余的变量。","created_at":"2018-07-25T05:43:17Z","updated_at":"2018-07-25T05:53:32Z","html_url":"https://github.com/TheMasterOfMagic/zlbweb/pull/4#discussion_r204986689","pull_request_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/4","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/comments/204986689"},"html":{"href":"https://github.com/TheMasterOfMagic/zlbweb/pull/4#discussion_r204986689"},"pull_request":{"href":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/4"}}},"pull_request":{"url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/4","id":203544328,"node_id":"MDExOlB1bGxSZXF1ZXN0MjAzNTQ0MzI4","html_url":"https://github.com/TheMasterOfMagic/zlbweb/pull/4","diff_url":"https://github.com/TheMasterOfMagic/zlbweb/pull/4.diff","patch_url":"https://github.com/TheMasterOfMagic/zlbweb/pull/4.patch","issue_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/issues/4","number":4,"state":"open","locked":false,"title":"电脑重装，重新初始化仓库，在之前代码的基础上增加提交表单改用ajax实现，hash存储使用了sha512算算法","user":{"login":"Lyc-heng","id":26518269,"node_id":"MDQ6VXNlcjI2NTE4MjY5","avatar_url":"https://avatars3.githubusercontent.com/u/26518269?v=4","gravatar_id":"","url":"https://api.github.com/users/Lyc-heng","html_url":"https://github.com/Lyc-heng","followers_url":"https://api.github.com/users/Lyc-heng/followers","following_url":"https://api.github.com/users/Lyc-heng/following{/other_user}","gists_url":"https://api.github.com/users/Lyc-heng/gists{/gist_id}","starred_url":"https://api.github.com/users/Lyc-heng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Lyc-heng/subscriptions","organizations_url":"https://api.github.com/users/Lyc-heng/orgs","repos_url":"https://api.github.com/users/Lyc-heng/repos","events_url":"https://api.github.com/users/Lyc-heng/events{/privacy}","received_events_url":"https://api.github.com/users/Lyc-heng/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-07-24T14:21:31Z","updated_at":"2018-07-25T05:53:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"340ae1e69915fcc469ec5cca97b09601429439ac","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/4/commits","review_comments_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/4/comments","review_comment_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/comments{/number}","comments_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/issues/4/comments","statuses_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/statuses/6a2977830e14dba526d01a4f77b3e9c01f8b2d34","head":{"label":"Lyc-heng:master","ref":"master","sha":"6a2977830e14dba526d01a4f77b3e9c01f8b2d34","user":{"login":"Lyc-heng","id":26518269,"node_id":"MDQ6VXNlcjI2NTE4MjY5","avatar_url":"https://avatars3.githubusercontent.com/u/26518269?v=4","gravatar_id":"","url":"https://api.github.com/users/Lyc-heng","html_url":"https://github.com/Lyc-heng","followers_url":"https://api.github.com/users/Lyc-heng/followers","following_url":"https://api.github.com/users/Lyc-heng/following{/other_user}","gists_url":"https://api.github.com/users/Lyc-heng/gists{/gist_id}","starred_url":"https://api.github.com/users/Lyc-heng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Lyc-heng/subscriptions","organizations_url":"https://api.github.com/users/Lyc-heng/orgs","repos_url":"https://api.github.com/users/Lyc-heng/repos","events_url":"https://api.github.com/users/Lyc-heng/events{/privacy}","received_events_url":"https://api.github.com/users/Lyc-heng/received_events","type":"User","site_admin":false},"repo":{"id":141220530,"node_id":"MDEwOlJlcG9zaXRvcnkxNDEyMjA1MzA=","name":"zlbweb","full_name":"Lyc-heng/zlbweb","owner":{"login":"Lyc-heng","id":26518269,"node_id":"MDQ6VXNlcjI2NTE4MjY5","avatar_url":"https://avatars3.githubusercontent.com/u/26518269?v=4","gravatar_id":"","url":"https://api.github.com/users/Lyc-heng","html_url":"https://github.com/Lyc-heng","followers_url":"https://api.github.com/users/Lyc-heng/followers","following_url":"https://api.github.com/users/Lyc-heng/following{/other_user}","gists_url":"https://api.github.com/users/Lyc-heng/gists{/gist_id}","starred_url":"https://api.github.com/users/Lyc-heng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Lyc-heng/subscriptions","organizations_url":"https://api.github.com/users/Lyc-heng/orgs","repos_url":"https://api.github.com/users/Lyc-heng/repos","events_url":"https://api.github.com/users/Lyc-heng/events{/privacy}","received_events_url":"https://api.github.com/users/Lyc-heng/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Lyc-heng/zlbweb","description":"小学期：密码学应用实践","fork":true,"url":"https://api.github.com/repos/Lyc-heng/zlbweb","forks_url":"https://api.github.com/repos/Lyc-heng/zlbweb/forks","keys_url":"https://api.github.com/repos/Lyc-heng/zlbweb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Lyc-heng/zlbweb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Lyc-heng/zlbweb/teams","hooks_url":"https://api.github.com/repos/Lyc-heng/zlbweb/hooks","issue_events_url":"https://api.github.com/repos/Lyc-heng/zlbweb/issues/events{/number}","events_url":"https://api.github.com/repos/Lyc-heng/zlbweb/events","assignees_url":"https://api.github.com/repos/Lyc-heng/zlbweb/assignees{/user}","branches_url":"https://api.github.com/repos/Lyc-heng/zlbweb/branches{/branch}","tags_url":"https://api.github.com/repos/Lyc-heng/zlbweb/tags","blobs_url":"https://api.github.com/repos/Lyc-heng/zlbweb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Lyc-heng/zlbweb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Lyc-heng/zlbweb/git/refs{/sha}","trees_url":"https://api.github.com/repos/Lyc-heng/zlbweb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Lyc-heng/zlbweb/statuses/{sha}","languages_url":"https://api.github.com/repos/Lyc-heng/zlbweb/languages","stargazers_url":"https://api.github.com/repos/Lyc-heng/zlbweb/stargazers","contributors_url":"https://api.github.com/repos/Lyc-heng/zlbweb/contributors","subscribers_url":"https://api.github.com/repos/Lyc-heng/zlbweb/subscribers","subscription_url":"https://api.github.com/repos/Lyc-heng/zlbweb/subscription","commits_url":"https://api.github.com/repos/Lyc-heng/zlbweb/commits{/sha}","git_commits_url":"https://api.github.com/repos/Lyc-heng/zlbweb/git/commits{/sha}","comments_url":"https://api.github.com/repos/Lyc-heng/zlbweb/comments{/number}","issue_comment_url":"https://api.github.com/repos/Lyc-heng/zlbweb/issues/comments{/number}","contents_url":"https://api.github.com/repos/Lyc-heng/zlbweb/contents/{+path}","compare_url":"https://api.github.com/repos/Lyc-heng/zlbweb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Lyc-heng/zlbweb/merges","archive_url":"https://api.github.com/repos/Lyc-heng/zlbweb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Lyc-heng/zlbweb/downloads","issues_url":"https://api.github.com/repos/Lyc-heng/zlbweb/issues{/number}","pulls_url":"https://api.github.com/repos/Lyc-heng/zlbweb/pulls{/number}","milestones_url":"https://api.github.com/repos/Lyc-heng/zlbweb/milestones{/number}","notifications_url":"https://api.github.com/repos/Lyc-heng/zlbweb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Lyc-heng/zlbweb/labels{/name}","releases_url":"https://api.github.com/repos/Lyc-heng/zlbweb/releases{/id}","deployments_url":"https://api.github.com/repos/Lyc-heng/zlbweb/deployments","created_at":"2018-07-17T02:32:34Z","updated_at":"2018-07-24T14:18:20Z","pushed_at":"2018-07-24T14:18:17Z","git_url":"git://github.com/Lyc-heng/zlbweb.git","ssh_url":"git@github.com:Lyc-heng/zlbweb.git","clone_url":"https://github.com/Lyc-heng/zlbweb.git","svn_url":"https://github.com/Lyc-heng/zlbweb","homepage":null,"size":168,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"TheMasterOfMagic:master","ref":"master","sha":"e614a783a84cd6f10891c816875dc56decd59faf","user":{"login":"TheMasterOfMagic","id":25054321,"node_id":"MDQ6VXNlcjI1MDU0MzIx","avatar_url":"https://avatars2.githubusercontent.com/u/25054321?v=4","gravatar_id":"","url":"https://api.github.com/users/TheMasterOfMagic","html_url":"https://github.com/TheMasterOfMagic","followers_url":"https://api.github.com/users/TheMasterOfMagic/followers","following_url":"https://api.github.com/users/TheMasterOfMagic/following{/other_user}","gists_url":"https://api.github.com/users/TheMasterOfMagic/gists{/gist_id}","starred_url":"https://api.github.com/users/TheMasterOfMagic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheMasterOfMagic/subscriptions","organizations_url":"https://api.github.com/users/TheMasterOfMagic/orgs","repos_url":"https://api.github.com/users/TheMasterOfMagic/repos","events_url":"https://api.github.com/users/TheMasterOfMagic/events{/privacy}","received_events_url":"https://api.github.com/users/TheMasterOfMagic/received_events","type":"User","site_admin":false},"repo":{"id":141211901,"node_id":"MDEwOlJlcG9zaXRvcnkxNDEyMTE5MDE=","name":"zlbweb","full_name":"TheMasterOfMagic/zlbweb","owner":{"login":"TheMasterOfMagic","id":25054321,"node_id":"MDQ6VXNlcjI1MDU0MzIx","avatar_url":"https://avatars2.githubusercontent.com/u/25054321?v=4","gravatar_id":"","url":"https://api.github.com/users/TheMasterOfMagic","html_url":"https://github.com/TheMasterOfMagic","followers_url":"https://api.github.com/users/TheMasterOfMagic/followers","following_url":"https://api.github.com/users/TheMasterOfMagic/following{/other_user}","gists_url":"https://api.github.com/users/TheMasterOfMagic/gists{/gist_id}","starred_url":"https://api.github.com/users/TheMasterOfMagic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheMasterOfMagic/subscriptions","organizations_url":"https://api.github.com/users/TheMasterOfMagic/orgs","repos_url":"https://api.github.com/users/TheMasterOfMagic/repos","events_url":"https://api.github.com/users/TheMasterOfMagic/events{/privacy}","received_events_url":"https://api.github.com/users/TheMasterOfMagic/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/TheMasterOfMagic/zlbweb","description":"小学期：密码学应用实践","fork":false,"url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb","forks_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/forks","keys_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/teams","hooks_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/hooks","issue_events_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/issues/events{/number}","events_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/events","assignees_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/assignees{/user}","branches_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/branches{/branch}","tags_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/tags","blobs_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/git/refs{/sha}","trees_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/statuses/{sha}","languages_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/languages","stargazers_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/stargazers","contributors_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/contributors","subscribers_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/subscribers","subscription_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/subscription","commits_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/commits{/sha}","git_commits_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/git/commits{/sha}","comments_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/comments{/number}","issue_comment_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/issues/comments{/number}","contents_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/contents/{+path}","compare_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/merges","archive_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/downloads","issues_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/issues{/number}","pulls_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls{/number}","milestones_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/milestones{/number}","notifications_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/labels{/name}","releases_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/releases{/id}","deployments_url":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/deployments","created_at":"2018-07-17T01:00:50Z","updated_at":"2018-07-19T02:59:43Z","pushed_at":"2018-07-24T14:21:32Z","git_url":"git://github.com/TheMasterOfMagic/zlbweb.git","ssh_url":"git@github.com:TheMasterOfMagic/zlbweb.git","clone_url":"https://github.com/TheMasterOfMagic/zlbweb.git","svn_url":"https://github.com/TheMasterOfMagic/zlbweb","homepage":null,"size":11,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"archived":false,"open_issues_count":1,"license":null,"forks":2,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/4"},"html":{"href":"https://github.com/TheMasterOfMagic/zlbweb/pull/4"},"issue":{"href":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/issues/4"},"comments":{"href":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/issues/4/comments"},"review_comments":{"href":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/4/comments"},"review_comment":{"href":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/pulls/4/commits"},"statuses":{"href":"https://api.github.com/repos/TheMasterOfMagic/zlbweb/statuses/6a2977830e14dba526d01a4f77b3e9c01f8b2d34"}},"author_association":"NONE"}}
{"actor":{"display_login":"TheMasterOfMagic"}}