mirror of
https://github.com/ONLYOFFICE/document-server-integration.git
synced 2026-04-07 14:06:11 +08:00
Merge remote-tracking branch 'remotes/origin/develop' into feature/fix-filename
# Conflicts: # web/documentserver-example/java/src/main/java/controllers/IndexServlet.java # web/documentserver-example/python/src/views/actions.py
This commit is contained in:
@ -31,6 +31,7 @@ import shutil
|
||||
import io
|
||||
import re
|
||||
import requests
|
||||
import time
|
||||
|
||||
from src import settings
|
||||
from . import fileUtils, historyManager
|
||||
@ -102,15 +103,21 @@ def getCorrectName(filename, req):
|
||||
|
||||
return name
|
||||
|
||||
def getFileUri(filename, req):
|
||||
host = config.EXAMPLE_DOMAIN.rstrip('/')
|
||||
def getServerUrl (forDocumentServer, req):
|
||||
if (forDocumentServer and config.EXAMPLE_DOMAIN is not None):
|
||||
return config.EXAMPLE_DOMAIN
|
||||
else:
|
||||
return req.headers.get("x-forwarded-proto") or req.scheme + "://" + req.get_host()
|
||||
|
||||
def getFileUri(filename, forDocumentServer, req):
|
||||
host = getServerUrl(forDocumentServer, req)
|
||||
curAdr = req.META['REMOTE_ADDR']
|
||||
return f'{host}{settings.STATIC_URL}{curAdr}/{filename}'
|
||||
|
||||
def getCallbackUrl(filename, req):
|
||||
host = config.EXAMPLE_DOMAIN
|
||||
host = getServerUrl(True, req)
|
||||
curAdr = req.META['REMOTE_ADDR']
|
||||
return f'{host}track?filename={filename}&userAddress={curAdr}'
|
||||
return f'{host}/track?filename={filename}&userAddress={curAdr}'
|
||||
|
||||
def getRootFolder(req):
|
||||
if isinstance(req, str):
|
||||
@ -140,7 +147,7 @@ def getStoredFiles(req):
|
||||
|
||||
for f in files:
|
||||
if os.path.isfile(os.path.join(directory, f)):
|
||||
fileInfos.append({ 'type': fileUtils.getFileType(f), 'title': f, 'url': getFileUri(f, req) })
|
||||
fileInfos.append({ 'type': fileUtils.getFileType(f), 'title': f, 'url': getFileUri(f, True, req) })
|
||||
|
||||
return fileInfos
|
||||
|
||||
@ -173,7 +180,7 @@ def createSample(fileType, sample, req):
|
||||
filename = getCorrectName(f'{sampleName}{ext}', req)
|
||||
path = getStoragePath(filename, req)
|
||||
|
||||
with io.open(os.path.join('samples', f'{sampleName}{ext}'), 'rb') as stream:
|
||||
with io.open(os.path.join('assets', 'sample' if sample == 'true' else 'new', f'{sampleName}{ext}'), 'rb') as stream:
|
||||
createFile(stream, path, req, True)
|
||||
return filename
|
||||
|
||||
@ -187,9 +194,34 @@ def removeFile(filename, req):
|
||||
|
||||
def generateFileKey(filename, req):
|
||||
path = getStoragePath(filename, req)
|
||||
uri = getFileUri(filename, req)
|
||||
uri = getFileUri(filename, False, req)
|
||||
stat = os.stat(path)
|
||||
|
||||
h = str(hash(f'{uri}_{stat.st_mtime_ns}'))
|
||||
replaced = re.sub(r'[^0-9-.a-zA-Z_=]', '_', h)
|
||||
return replaced[:20]
|
||||
|
||||
def getFilesInfo(req):
|
||||
fileId = req.GET.get('fileId') if req.GET.get('fileId') else None
|
||||
|
||||
result = []
|
||||
resultID = []
|
||||
for f in getStoredFiles(req):
|
||||
stats = os.stat(os.path.join(getRootFolder(req), f.get("title")))
|
||||
result.append(
|
||||
{ "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 fileId == generateFileKey(f.get("title"), req) :
|
||||
resultID.append(result[-1])
|
||||
|
||||
if fileId :
|
||||
if len(resultID) > 0 : return resultID
|
||||
else : return "File not found"
|
||||
else :
|
||||
return result
|
||||
@ -32,6 +32,8 @@ import config
|
||||
from . import users, fileUtils
|
||||
from datetime import datetime
|
||||
from src import settings
|
||||
from src.utils import docManager
|
||||
from src.utils import jwtManager
|
||||
|
||||
def getHistoryDir(storagePath):
|
||||
return f'{storagePath}-hist'
|
||||
@ -43,7 +45,7 @@ def getFileVersion(histDir):
|
||||
if not os.path.exists(histDir):
|
||||
return 0
|
||||
|
||||
cnt = 0
|
||||
cnt = 1
|
||||
|
||||
for f in os.listdir(histDir):
|
||||
if not os.path.isfile(os.path.join(histDir, f)):
|
||||
@ -53,7 +55,7 @@ def getFileVersion(histDir):
|
||||
|
||||
def getNextVersionDir(histDir):
|
||||
v = getFileVersion(histDir)
|
||||
path = getVersionDir(histDir, v + 1)
|
||||
path = getVersionDir(histDir, v)
|
||||
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
@ -84,7 +86,7 @@ def createMeta(storagePath, req):
|
||||
user = users.getUserFromReq(req)
|
||||
|
||||
obj = {
|
||||
'created': datetime.today().strftime('%d.%m.%Y %H:%M:%S'),
|
||||
'created': datetime.today().strftime('%Y-%m-%d %H:%M:%S'),
|
||||
'uid': user['uid'],
|
||||
'uname': user['uname']
|
||||
}
|
||||
@ -103,12 +105,12 @@ def readFile(path):
|
||||
return stream.read()
|
||||
|
||||
def getPrevUri(filename, ver, ext, req):
|
||||
host = config.EXAMPLE_DOMAIN.rstrip('/')
|
||||
host = docManager.getServerUrl(True, req)
|
||||
curAdr = req.META['REMOTE_ADDR']
|
||||
return f'{host}{settings.STATIC_URL}{curAdr}/{filename}-hist/{ver}/prev{ext}'
|
||||
|
||||
def getZipUri(filename, ver, req):
|
||||
host = config.EXAMPLE_DOMAIN.rstrip('/')
|
||||
host = docManager.getServerUrl(True, req)
|
||||
curAdr = req.META['REMOTE_ADDR']
|
||||
return f'{host}{settings.STATIC_URL}{curAdr}/{filename}-hist/{ver}/diff.zip'
|
||||
|
||||
@ -128,12 +130,12 @@ def getHistoryObject(storagePath, filename, docKey, docUrl, req):
|
||||
if version > 0:
|
||||
hist = []
|
||||
histData = {}
|
||||
|
||||
for i in range(version + 1):
|
||||
|
||||
for i in range(1, version + 1):
|
||||
obj = {}
|
||||
dataObj = {}
|
||||
prevVerDir = getVersionDir(histDir, i)
|
||||
verDir = getVersionDir(histDir, i + 1)
|
||||
prevVerDir = getVersionDir(histDir, i - 1)
|
||||
verDir = getVersionDir(histDir, i)
|
||||
|
||||
try:
|
||||
key = docKey if i == version else readFile(getKeyPath(verDir))
|
||||
@ -143,7 +145,7 @@ def getHistoryObject(storagePath, filename, docKey, docUrl, req):
|
||||
dataObj['key'] = key
|
||||
dataObj['version'] = i
|
||||
|
||||
if i == 0:
|
||||
if i == 1:
|
||||
meta = getMeta(storagePath)
|
||||
if meta:
|
||||
obj['created'] = meta['created']
|
||||
@ -152,9 +154,9 @@ def getHistoryObject(storagePath, filename, docKey, docUrl, req):
|
||||
'name': meta['uname']
|
||||
}
|
||||
|
||||
dataObj['url'] = docUrl if i == version else getPrevUri(filename, i + 1, fileUtils.getFileExt(filename), req)
|
||||
dataObj['url'] = docUrl if i == version else getPrevUri(filename, i, fileUtils.getFileExt(filename), req)
|
||||
|
||||
if i > 0:
|
||||
if i > 1:
|
||||
changes = json.loads(readFile(getChangesHistoryPath(prevVerDir)))
|
||||
change = changes['changes'][0]
|
||||
|
||||
@ -163,16 +165,19 @@ def getHistoryObject(storagePath, filename, docKey, docUrl, req):
|
||||
obj['created'] = change['created']
|
||||
obj['user'] = change['user']
|
||||
|
||||
prev = histData[str(i - 1)]
|
||||
prev = histData[str(i - 2)]
|
||||
prevInfo = {
|
||||
'key': prev['key'],
|
||||
'url': prev['url']
|
||||
}
|
||||
dataObj['previous'] = prevInfo
|
||||
dataObj['changesUrl'] = getZipUri(filename, i, req)
|
||||
dataObj['changesUrl'] = getZipUri(filename, i - 1, req)
|
||||
|
||||
if jwtManager.isEnabled():
|
||||
dataObj['token'] = jwtManager.encode(dataObj)
|
||||
|
||||
hist.append(obj)
|
||||
histData[str(i)] = dataObj
|
||||
histData[str(i - 1)] = dataObj
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
@ -50,11 +50,12 @@ def getConverterUri(docUri, fromExt, toExt, docKey, isAsync):
|
||||
payload.setdefault('async', True)
|
||||
|
||||
if jwtManager.isEnabled():
|
||||
jwtHeader = 'Authorization' if config.DOC_SERV_JWT_HEADER is None or config.DOC_SERV_JWT_HEADER == '' else config.DOC_SERV_JWT_HEADER
|
||||
headerToken = jwtManager.encode({'payload': payload})
|
||||
payload['token'] = jwtManager.encode(payload)
|
||||
headers['Authorization'] = f'Bearer {headerToken}'
|
||||
headers[jwtHeader] = f'Bearer {headerToken}'
|
||||
|
||||
response = requests.post(config.DOC_SERV_CONVERTER_URL, json=payload, headers=headers )
|
||||
response = requests.post(config.DOC_SERV_SITE_URL + config.DOC_SERV_CONVERTER_URL, json=payload, headers=headers )
|
||||
json = response.json()
|
||||
|
||||
return getResponseUri(json)
|
||||
|
||||
Reference in New Issue
Block a user