|
|
@@ -3,7 +3,10 @@
|
3
|
3
|
import errno
|
4
|
4
|
import glob
|
5
|
5
|
import os
|
|
6
|
+import re
|
6
|
7
|
import sqlite3
|
|
8
|
+import stat
|
|
9
|
+import time
|
7
|
10
|
from functools import wraps
|
8
|
11
|
|
9
|
12
|
from tornado.httpserver import HTTPServer
|
|
|
@@ -47,7 +50,7 @@ SELECT_ORIGIN_PATH_STMT = 'SELECT origin_path FROM photoinfo WHERE lensman = ? a
|
47
|
50
|
conn = sqlite3.connect('minipai2.db')
|
48
|
51
|
cur = conn.cursor()
|
49
|
52
|
# Synchronous Off
|
50
|
|
-cur.execute('PRAGMA synchronous = OFF')
|
|
53
|
+# cur.execute('PRAGMA synchronous = OFF')
|
51
|
54
|
# Execute SQL
|
52
|
55
|
cur.execute(CREATE_TABLE_STMT)
|
53
|
56
|
cur.execute(CREATE_INDEX1)
|
|
|
@@ -111,8 +114,13 @@ def get_last_timestamp(lensman, session):
|
111
|
114
|
|
112
|
115
|
|
113
|
116
|
def insert_session_file(files_):
|
|
117
|
+ start_at = time.time()
|
114
|
118
|
cur.executemany(INSERT_RECORD_STMT, files_)
|
115
|
119
|
conn.commit()
|
|
120
|
+ end_at = time.time()
|
|
121
|
+ logit('/fetch_thumbnail', start_at, key='start_at')
|
|
122
|
+ logit('/fetch_thumbnail', end_at, key='end_at')
|
|
123
|
+ logit('/fetch_thumbnail', end_at - start_at, key='delta_time')
|
116
|
124
|
|
117
|
125
|
|
118
|
126
|
def delete_session_file(lensman, session, name):
|
|
|
@@ -120,38 +128,66 @@ def delete_session_file(lensman, session, name):
|
120
|
128
|
conn.commit()
|
121
|
129
|
|
122
|
130
|
|
123
|
|
-def get_new_files(lensman, session, maxid, maxt):
|
|
131
|
+def get_file_info(file_):
|
|
132
|
+ photo_name = file_.split('/')[-1]
|
|
133
|
+ photo_id = photo_name.split('.')[0]
|
|
134
|
+ thumb_path = file_.replace('{}/'.format(ROOT_PATH), '')
|
|
135
|
+ origin_path = thumb_path.replace('thumbnail', 'origin')
|
|
136
|
+ return photo_id, photo_name, thumb_path, origin_path
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+def is_file_valid(file_):
|
|
140
|
+ if file_.endswith('.tmp'): # Whether 'xxx.tmp' or not
|
|
141
|
+ return False
|
|
142
|
+ if not re.match(r'.*\/\d+\.\w+', file_): # Whether 'xxx/digit.xxx' or not
|
|
143
|
+ return False
|
|
144
|
+ if not os.stat(file_).st_size: # Whether `file size zero` or not
|
|
145
|
+ return False
|
|
146
|
+ return True
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+def get_glob_files(lensman, session):
|
124
|
150
|
_, thumb = get_session_dir(lensman, session)
|
125
|
|
- files = glob.iglob('{}/*'.format(thumb))
|
|
151
|
+ return glob.iglob('{}/*'.format(thumb))
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+def get_all_files(lensman, session):
|
|
155
|
+ # Glob Files
|
|
156
|
+ files = get_glob_files(lensman, session)
|
|
157
|
+ # Init Vars
|
|
158
|
+ files_ = []
|
|
159
|
+ # Foreach Glob Files
|
|
160
|
+ for file_ in files:
|
|
161
|
+ logit('/session_end', file_, key='globfile')
|
|
162
|
+ if is_file_valid(file_):
|
|
163
|
+ photo_id, photo_name, thumb_path, origin_path = get_file_info(file_)
|
|
164
|
+ # Insert When Photo_id Large Than Maxt
|
|
165
|
+ files_.append((lensman, session, photo_id, photo_name, thumb_path, origin_path))
|
|
166
|
+ # If Having Files To Insert Into SQLite
|
|
167
|
+ if files_:
|
|
168
|
+ insert_session_file(files_)
|
|
169
|
+ return files_
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+def get_new_files(lensman, session, maxid):
|
|
173
|
+ # Glob Files
|
|
174
|
+ files = get_glob_files(lensman, session)
|
126
|
175
|
# Init Vars
|
127
|
|
- news, files_ = [], []
|
|
176
|
+ files_ = []
|
128
|
177
|
# Foreach Glob Files
|
129
|
178
|
for file_ in files:
|
130
|
179
|
logit('/fetch_thumbnail', file_, key='globfile')
|
131
|
|
- if not file_.endswith('.tmp'): # Whether 'xxx.tmp' or not
|
132
|
|
- # Photo Name
|
133
|
|
- photo_name = file_.split('/')[-1]
|
134
|
|
- # Photo_id
|
135
|
|
- photo_id = photo_name.split('.')[0]
|
136
|
|
- # Thumb Path
|
137
|
|
- thumb_path = file_.replace('{}/'.format(ROOT_PATH), '')
|
138
|
|
- # Origin Path
|
139
|
|
- origin_path = thumb_path.replace('thumbnail', 'origin')
|
|
180
|
+ if is_file_valid(file_):
|
|
181
|
+ photo_id, photo_name, thumb_path, _ = get_file_info(file_)
|
140
|
182
|
# Return When Photo_id Large Than Maxid
|
141
|
183
|
# Solve APP Doesn't Get Photo When Timeout
|
142
|
184
|
if int(photo_id) > maxid:
|
143
|
|
- news.append({
|
|
185
|
+ files_.append({
|
144
|
186
|
'id': photo_id,
|
145
|
187
|
'name': photo_name,
|
146
|
188
|
'path': thumb_path,
|
147
|
189
|
})
|
148
|
|
- # Insert When Photo_id Large Than Maxt
|
149
|
|
- if int(photo_id) > maxt:
|
150
|
|
- files_.append((lensman, session, photo_id, photo_name, thumb_path, origin_path))
|
151
|
|
- # If Having Files To Insert Into SQLite
|
152
|
|
- if files_:
|
153
|
|
- insert_session_file(files_)
|
154
|
|
- return news
|
|
190
|
+ return files_
|
155
|
191
|
|
156
|
192
|
|
157
|
193
|
def get_origin_path_from_id(lensman, session, id_):
|
|
|
@@ -195,8 +231,14 @@ class SessionEndHandler(RequestHandler):
|
195
|
231
|
lensman = self.get_argument('lensman', '')
|
196
|
232
|
session = self.get_argument('session', '')
|
197
|
233
|
|
198
|
|
- # Change Mode to 0700
|
199
|
|
- os.chmod(get_session_root(lensman, session), 0700)
|
|
234
|
+ # Session Root
|
|
235
|
+ session_root = get_session_root(lensman, session)
|
|
236
|
+ # Whether Mode Has Changed
|
|
237
|
+ if stat.S_IMODE(os.stat(session_root).st_mode) != 0700:
|
|
238
|
+ # Insert Files Into SQLite
|
|
239
|
+ get_all_files(lensman, session)
|
|
240
|
+ # Change Mode to 0700
|
|
241
|
+ os.chmod(session_root, 0700)
|
200
|
242
|
|
201
|
243
|
self.write({
|
202
|
244
|
'status': 200,
|
|
|
@@ -210,10 +252,10 @@ class FetchThumbnailHandler(RequestHandler):
|
210
|
252
|
session = self.get_argument('session', '')
|
211
|
253
|
maxid = int(self.get_argument('maxid', 0))
|
212
|
254
|
|
213
|
|
- maxt = get_last_timestamp(lensman, session)
|
214
|
|
- files = get_new_files(lensman, session, maxid or maxt, maxt)
|
|
255
|
+ # New Files
|
|
256
|
+ files = get_new_files(lensman, session, maxid)
|
215
|
257
|
|
216
|
|
- logit(self, maxt, key='maxt')
|
|
258
|
+ # logit(self, maxt, key='maxt')
|
217
|
259
|
logit(self, files, key='files')
|
218
|
260
|
|
219
|
261
|
self.write({
|