-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
76 lines (58 loc) · 1.62 KB
/
control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*- #
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from werkzeug.utils import secure_filename
import re
import sqlite3
import StringIO
import markdown
import codecs
def secure_cn_filename(s):
s = re.sub('[" "\/\--.]+', '-', s)
s = re.sub(r':-', ':', s)
s = re.sub(r'^-|-$', '', s)
return s
def initDB():
try:
c = sqlite3.connect('resource.db').cursor()
c.execute('''
CREATE TABLE resource(
uid INTEGER PRIMARY KEY NOT NULL,
file BLOB NOT NULL,
filename TEXT NOT NULL,
filetype TEXT NOT NULL)''')
c.commit()
except:
pass
#初始化数据库
#initDB()
def SaveToDB(files, filename):
conn = sqlite3.connect('resource.db')
c = conn.cursor()
c.execute('SELECT max(uid) FROM resource')
try:
uid = c.fetchone()[0] + 1
except TypeError:
uid = 1
filetype = secure_filename(filename).split('.')[-1].lower()
c.execute('INSERT INTO resource VALUES (?, ?, ?, ?)', (uid, sqlite3.Binary(files.read()), filename, filetype))
conn.commit()
return uid
def ReadDB(uid):
conn = sqlite3.connect('resource.db')
c = conn.cursor()
c.execute('SELECT uid, file, filename, filetype FROM resource WHERE uid = %s' % str(uid))
return c.fetchone()
def md(files):
files = codecs.open(files, mode="r", encoding="utf-8")
text = files.read()
html = markdown.markdown(text)
return html
if __name__ == '__main__':
#for test
#files = open('test.png', 'rb')
#SaveToDB(files, 'test.png')
#uid = 10
#print ReadDB(uid)
initDB()