瀏覽代碼

파일 업로드 기능

2DU 9 年之前
父節點
當前提交
33a0a38279
共有 4 個文件被更改,包括 48 次插入7 次删除
  1. 35 4
      app.py
  2. 二進制
      image/mc.png
  3. 二進制
      image/namu.png
  4. 13 3
      templates/index.html

+ 35 - 4
app.py

@@ -1,4 +1,5 @@
-from flask import Flask, request, session, render_template
+from flask import Flask, request, session, render_template, send_file
+from werkzeug import secure_filename
 app = Flask(__name__)
 
 from urllib import parse
@@ -7,6 +8,7 @@ import pymysql
 import time
 import re
 import bcrypt
+import os
 
 json_data = open('set.json').read()
 data = json.loads(json_data)
@@ -16,6 +18,12 @@ curs = conn.cursor(pymysql.cursors.DictCursor)
 
 app.secret_key = data['key']
 
+ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
+
+def allowed_file(filename):
+    return '.' in filename and \
+           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
+
 def namumark(title, data):
     data = re.sub('<', '&lt;', data)
     data = re.sub('>', '&gt;', data)
@@ -156,6 +164,8 @@ def namumark(title, data):
     
     data = re.sub("##\s?(?P<in>[^\n]*)\n", "<div style='display:none;'>\g<in></div>", data);
     
+    data = re.sub("\[\[파일:(?P<in>(?:(?!\]\]).)*)\]\]", "<img src='/image/\g<in>'>", data)
+    
     data = re.sub("\[br\]",'<br>', data);
     
     while True:
@@ -398,7 +408,28 @@ def getleng(existing, change):
     else:
         leng = '0'
     return leng;
-
+    
+    
+@app.route('/upload', methods=['GET', 'POST'])
+def upload():
+    if(request.method == 'POST'):
+        file = request.files['file']
+        if(file and allowed_file(file.filename)):
+            filename = secure_filename(file.filename)
+            if(os.path.exists(os.path.join('image', filename))):
+                return render_template('index.html', logo = data['name'], title = '업로드', data = '<a href="/upload">동일한 이름 파일 있음</a>')
+            else:
+                file.save(os.path.join('image', filename))
+                return render_template('index.html', logo = data['name'], title = '업로드', data = '<a href="/upload">완료 됨</a>')
+        else:
+            return render_template('index.html', logo = data['name'], title = '업로드', data = '<a href="/upload">jpg gif jpeg png만 가능 합니다.</a>')
+    return render_template('index.html', logo = data['name'], title = '업로드', tn = 21)
+    
+@app.route('/image/<name>')
+def image(name = None):
+    filename = secure_filename(name)
+    return send_file(os.path.join('image', filename), mimetype='image')
+    
 @app.route('/')
 @app.route('/w/')
 def redirect():
@@ -431,7 +462,7 @@ def recentchanges():
             i = i + 1
         return render_template('index.html', logo = data['name'], rows = div, tn = 3, title = '최근 변경내역')
     else:
-         return render_template('index.html', logo = data['name'], rows = '', tn = 3, title = '최근 변경내역')
+        return render_template('index.html', logo = data['name'], rows = '', tn = 3, title = '최근 변경내역')
 
 @app.route('/recentdiscuss')
 def recentdiscuss():
@@ -796,7 +827,7 @@ def setup():
 
 @app.route('/other')
 def other():
-    return render_template('index.html', title = '기타 메뉴', logo = data['name'], data = '<li><a href="/titleindex">모든 문서</a><li><a href="/grammar">문법 설명</a></li><li><a href="/version">버전</a></li><li><a href="/recentblock">최근 차단내역</a></li>')
+    return render_template('index.html', title = '기타 메뉴', logo = data['name'], data = '<li><a href="/titleindex">모든 문서</a><li><a href="/grammar">문법 설명</a></li><li><a href="/version">버전</a></li><li><a href="/recentblock">최근 차단내역</a></li><li><a href="/upload">업로드</a></li>')
 
 @app.route('/titleindex')
 def titleindex():

二進制
image/mc.png


二進制
image/namu.png


+ 13 - 3
templates/index.html

@@ -304,9 +304,11 @@
                 <li>토론 목록 보강</li>
 				<li>대역 차단 구현</li>
 				<li>diff 구현</li>
-                <li>파일 업로드 구현</li>
+                <li>파일 업로드 구현 (완료)</li>
 				<li>관리자 기능의 접근성 향상</li>
 				<li>기본적인 다중 검사 기능</li>
+                <h2>1.3</h2>
+                <li>파일 업로드 개선</li>
 				<h2>계속</h2>
 				<li>버그 수정</li>
 				<li>파서 제작</li>
@@ -318,7 +320,7 @@
 				<li>일부 파일 <a href="https://twitter.com/Basix1120">Basix</a></li>
 				<br>
 				<h2>기타</h2>
-				<li>현재 버전 1.2.4 - Beta</li>
+				<li>현재 버전 1.2.6 - Beta</li>
 			</div>
 			{% elif tn == 15 %}
 			<h1 class="title">{{title}}</h1>
@@ -451,7 +453,15 @@
 			{% autoescape false %}
 			{{rows}}
 			{% endautoescape %}
-			{% else %}
+            {% elif tn == 21 %}
+			<h1 class="title">{{title}}</h1>
+			<div>
+				<form action="" method=post enctype=multipart/form-data>
+                    <input type=file name=file>
+                    <input type=submit value=Upload>
+                </form>
+			</div>
+            {% else %}
 			<h1 class="title">{{title}}</h1>
 			<div>
 				{% autoescape false %}