-
Notifications
You must be signed in to change notification settings - Fork 110
/
docManager.py
executable file
·305 lines (243 loc) · 11 KB
/
docManager.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
(c) Copyright Ascensio System SIA 2023
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import shutil
import io
import re
import requests
import time
import urllib.parse
import magic
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect, FileResponse
from src.configuration import ConfigurationManager
from . import fileUtils, historyManager
config_manager = ConfigurationManager()
def isCanFillForms(ext):
return ext in config_manager.fillable_file_extensions()
# check if the file extension can be viewed
def isCanView(ext):
return ext in config_manager.viewable_file_extensions()
# check if the file extension can be edited
def isCanEdit(ext):
return ext in config_manager.editable_file_extensions()
# check if the file extension can be converted
def isCanConvert(ext):
return ext in config_manager.convertible_file_extensions()
# check if the file extension is supported by the editor (it can be viewed or edited or converted)
def isSupportedExt(ext):
return isCanView(ext) | isCanEdit(ext) | isCanConvert(ext) | isCanFillForms(ext)
# get internal extension for a given file type
def getInternalExtension(fileType):
mapping = {
'word': '.docx',
'cell': '.xlsx',
'slide': '.pptx',
'docxf': '.docxf'
}
return mapping.get(fileType, '.docx') # the default file type is .docx
# get image url for templates
def getTemplateImageUrl(fileType, request):
path = getServerUrl(True, request) + '/static/images/'
mapping = {
'word': path + 'file_docx.svg',
'cell': path + 'file_xlsx.svg',
'slide': path + 'file_pptx.svg'
}
return mapping.get(fileType, path + 'file_docx.svg') # the default file type
# get file name with an index if such a file name already exists
def getCorrectName(filename, req):
basename = fileUtils.getFileNameWithoutExt(filename)
ext = fileUtils.getFileExt(filename)
name = f'{basename}{ext}'
i = 1
while os.path.exists(getStoragePath(name, req)): # if file with such a name already exists
name = f'{basename} ({i}){ext}' # add an index to its name
i += 1
return name
# get server url
def getServerUrl (forDocumentServer, req):
example_url = config_manager.example_url()
if (forDocumentServer and example_url is not None):
return example_url.geturl()
else:
return req.headers.get("x-forwarded-proto") or req.scheme + "://" + req.get_host()
# get file url
def getFileUri(filename, forDocumentServer, req):
host = getServerUrl(forDocumentServer, req)
curAdr = req.META['REMOTE_ADDR']
return f'{host}{settings.STATIC_URL}{curAdr}/{filename}'
# get absolute URL to the document storage service
def getCallbackUrl(filename, req):
host = getServerUrl(True, req)
curAdr = req.META['REMOTE_ADDR']
return f'{host}/track?filename={filename}&userAddress={curAdr}'
# get url to the created file
def getCreateUrl(fileType, req):
host = getServerUrl(False, req)
return f'{host}/create?fileType={fileType}'
# get url to download a file
def getDownloadUrl(filename, req, isServerUrl = True):
host = getServerUrl(isServerUrl, req)
curAdr = f'&userAddress={req.META["REMOTE_ADDR"]}' if isServerUrl else ""
return f'{host}/download?fileName={filename}{curAdr}'
# get root folder for the current file
def getRootFolder(req):
if isinstance(req, str):
curAdr = req
else:
curAdr = req.META['REMOTE_ADDR']
storage_directory = config_manager.storage_path()
directory = storage_directory.joinpath(curAdr)
if not os.path.exists(directory): # if such a directory does not exist, make it
os.makedirs(directory)
return directory
# get the file history path
def getHistoryPath(filename, file, version, req):
if isinstance(req, str):
curAdr = req
else:
curAdr = req.META['REMOTE_ADDR']
storage_directory = config_manager.storage_path()
directory = storage_directory.joinpath(curAdr)
if not os.path.exists(directory): # the directory with host address doesn't exist
filePath = os.path.join(getRootFolder(req), f'{filename}-hist', version, file)
else:
filePath = os.path.join(directory, f'{filename}-hist', version, file)
return filePath
# get the file path
def getStoragePath(filename, req):
directory = getRootFolder(req)
return os.path.join(directory, fileUtils.getFileName(filename))
# get the path to the forcesaved file version
def getForcesavePath(filename, req, create):
if isinstance(req, str):
curAdr = req
else:
curAdr = req.META['REMOTE_ADDR']
storage_directory = config_manager.storage_path()
directory = storage_directory.joinpath(curAdr)
if not os.path.exists(directory): # the directory with host address doesn't exist
return ""
directory = os.path.join(directory, f'{filename}-hist') # get the path to the history of the given file
if (not os.path.exists(directory)):
if create: # if the history directory doesn't exist
os.makedirs(directory) # create history directory if it doesn't exist
else: # the history directory doesn't exist and we are not supposed to create it
return ""
directory = os.path.join(directory, filename) # and get the path to the given file
if (not os.path.exists(directory) and not create):
return ""
return directory
# get information about all the stored files
def getStoredFiles(req):
directory = getRootFolder(req)
files = os.listdir(directory)
files.sort(key=lambda x: os.path.getmtime(os.path.join(directory, x)), reverse=True) # sort files by time of last modification
fileInfos = []
for f in files:
if os.path.isfile(os.path.join(directory, f)):
fileInfos.append({'isFillFormDoc': isCanFillForms(fileUtils.getFileExt(f)),'version':historyManager.getFileVersion(historyManager.getHistoryDir(getStoragePath(f, req))), 'type': fileUtils.getFileType(f), 'title': f, 'url': getFileUri(f, True, req), 'canEdit': isCanEdit(fileUtils.getFileExt(f))}) # write information about file type, title and url
return fileInfos
# create a file
def createFile(stream, path, req = None, meta = False):
bufSize = 8192
with io.open(path, 'wb') as out: # write data to the file by streams
read = stream.read(bufSize)
while len(read) > 0:
out.write(read)
read = stream.read(bufSize)
if meta:
historyManager.createMeta(path, req) # create meta data for the file if needed
return
# save file
def saveFile(response, path):
with open(path, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
return
# download file from the given url
def downloadFileFromUri(uri, path = None, withSave = False):
resp = requests.get(uri, stream=True, verify = config_manager.ssl_verify_peer_mode_enabled(), timeout=5)
status_code = resp.status_code
if status_code != 200: # checking status code
raise RuntimeError('Document editing service returned status: %s' % status_code)
if withSave:
if path is None:
raise RuntimeError('Path for saving file is null')
saveFile(resp, path)
return resp
# create sample file
def createSample(fileType, sample, req):
ext = getInternalExtension(fileType) # get the internal extension of the given file type
if not sample:
sample = 'false'
sampleName = 'sample' if sample == 'true' else 'new' # create sample or new template
filename = getCorrectName(f'{sampleName}{ext}', req) # get file name with an index if such a file name already exists
path = getStoragePath(filename, req)
with io.open(os.path.join('assets', 'sample' if sample == 'true' else 'new', f'{sampleName}{ext}'), 'rb') as stream: # create sample file of the necessary extension in the directory
createFile(stream, path, req, True)
return filename
# remove file from the directory
def removeFile(filename, req):
path = getStoragePath(filename, req)
if os.path.exists(path):
os.remove(path)
histDir = historyManager.getHistoryDir(path) # get history directory
if os.path.exists(histDir): # remove all the history information about this file
shutil.rmtree(histDir)
# generate file key
def generateFileKey(filename, req):
path = getStoragePath(filename, req)
uri = getFileUri(filename, False, req)
stat = os.stat(path) # get the directory parameters
h = str(hash(f'{uri}_{stat.st_mtime_ns}')) # get the hash value of the file url and the date of its last modification and turn it into a string format
replaced = re.sub(r'[^0-9-.a-zA-Z_=]', '_', h)
return replaced[:20] # take the first 20 characters for the key
# generate the document key value
def generateRevisionId(expectedKey):
if (len(expectedKey) > 20):
expectedKey = str(hash(expectedKey))
key = re.sub(r'[^0-9-.a-zA-Z_=]', '_', expectedKey)
return key[:20]
# get files information
def getFilesInfo(req):
fileId = req.GET.get('fileId') if req.GET.get('fileId') else None
result = []
resultID = []
for f in getStoredFiles(req): # run through all the files from the directory
stats = os.stat(os.path.join(getRootFolder(req), f.get("title"))) # get file information
result.append( # write file parameters to the file object
{ "version" : historyManager.getFileVersion(historyManager.getHistoryDir(getStoragePath(f.get("title"), req))),
"id" : generateFileKey(f.get("title"), req),
"contentLength" : "%.2f KB" % (stats.st_size/1024),
"pureContentLength" : stats.st_size,
"title" : f.get("title"),
"updated" : time.strftime("%Y-%m-%dT%X%z",time.gmtime(stats.st_mtime))
})
if fileId : # if file id is defined
if fileId == generateFileKey(f.get("title"), req) : # and it is equal to the file key value
resultID.append(result[-1]) # add file object to the response array
if fileId :
if len(resultID) > 0 : return resultID
else : return "File not found"
else :
return result
# download the file
def download(filePath):
response = FileResponse(open(filePath, 'rb'), True) # write headers to the response object
response['Content-Length'] = os.path.getsize(filePath)
response['Content-Disposition'] = "attachment;filename*=UTF-8\'\'" + urllib.parse.quote_plus(os.path.basename(filePath))
response['Content-Type'] = magic.from_file(filePath, mime=True)
response['Access-Control-Allow-Origin'] = "*"
return response