-
Notifications
You must be signed in to change notification settings - Fork 7
/
startup.py
82 lines (67 loc) · 2.04 KB
/
startup.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
77
78
79
80
81
82
# coding: UTF-8
from application import app, db, fs, mongo
import docs
import os, sys
def listDirRec(dirPath):
files = []
for name in os.listdir(dirPath):
path = os.path.join(dirPath, name)
if os.path.isfile(path):
files.append(path)
elif os.path.isdir(path):
map(files.append, listDirRec(path))
else:
raise Exception('unknown type of file: %s' % path)
return files
def init_db():
try:
#if app.debug: # 但开发告一段落时将这里改成 if app.debug: db.drop_all()以使只在开发模式下清空表,
db.drop_all() # 但之前这样做以使得无论本地开发还是部署到appfog等云上都可以正常运行(开发时)
db.create_all()
except:
pass
import models
root_dir = os.path.dirname(__file__)
resources_dir = os.path.join(root_dir, 'static', 'resources')
filepaths = listDirRec(resources_dir)
files = []
def getFileInfo(path):
relativePath = path.replace(resources_dir + os.path.sep, '').replace('\\', '/')
tmp = filter(lambda x: x != '', relativePath.split('/'))
pathname = '.'.join(tmp)
filename = tmp[-1]
return {
"path": path,
"relativePath": relativePath,
"pathname": pathname,
"filename": filename
}
def endsWithOneInArray(str, ends):
for end in ends:
l = len(end)
if str[-l:] == end:
return True
return False
def saveFileInfoToDb(fileInfo):
is_binary = False
if endsWithOneInArray(fileInfo['filename'].lower(), ['jpg', 'png', 'ico', 'jpeg', 'bmp']):
file_type = 'image'
is_binary = True
if is_binary:
f = open(fileInfo['path'], 'rb')
else:
f = open(fileInfo['path'], 'r')
data = f.read()
f.close()
blob_key = docs.Blob(data).save()
file_type = 'file'
if endsWithOneInArray(fileInfo['filename'].lower(), ['jpg', 'png', 'ico', 'jpeg', 'bmp']):
file_type = 'image'
resource = models.Resource(name=fileInfo['pathname'], type=file_type, blob_key=blob_key,
tags="file resource my-3d-format")
db.session.add(resource)
db.session.commit()
return True
# init_db()
# map(lambda x: files.append(getFileInfo(x)), filepaths)
# map(saveFileInfoToDb, files)