mirror of
https://github.com/haiwen/seahub.git
synced 2025-08-25 18:20:48 +00:00
update new office file
This commit is contained in:
parent
13fac0efa0
commit
5ccd21c718
BIN
media/office-template/empty.docx
Normal file
BIN
media/office-template/empty.docx
Normal file
Binary file not shown.
BIN
media/office-template/empty.pptx
Normal file
BIN
media/office-template/empty.pptx
Normal file
Binary file not shown.
BIN
media/office-template/empty.xlsx
Normal file
BIN
media/office-template/empty.xlsx
Normal file
Binary file not shown.
@ -2,6 +2,7 @@
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import posixpath
|
import posixpath
|
||||||
|
import requests
|
||||||
|
|
||||||
from rest_framework.authentication import SessionAuthentication
|
from rest_framework.authentication import SessionAuthentication
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
@ -15,11 +16,13 @@ from seahub.api2.throttling import UserRateThrottle
|
|||||||
from seahub.api2.authentication import TokenAuthentication
|
from seahub.api2.authentication import TokenAuthentication
|
||||||
from seahub.api2.utils import api_error
|
from seahub.api2.utils import api_error
|
||||||
|
|
||||||
from seahub.utils import check_filename_with_rename, is_pro_version
|
from seahub.utils import check_filename_with_rename, is_pro_version, \
|
||||||
|
gen_file_upload_url
|
||||||
from seahub.utils.timeutils import timestamp_to_isoformat_timestr
|
from seahub.utils.timeutils import timestamp_to_isoformat_timestr
|
||||||
from seahub.views import check_folder_permission, check_file_lock
|
from seahub.views import check_folder_permission, check_file_lock
|
||||||
|
|
||||||
from seahub.settings import MAX_UPLOAD_FILE_NAME_LEN, FILE_LOCK_EXPIRATION_DAYS
|
from seahub.settings import MAX_UPLOAD_FILE_NAME_LEN, \
|
||||||
|
FILE_LOCK_EXPIRATION_DAYS, OFFICE_TEMPLATE_ROOT
|
||||||
|
|
||||||
from seaserv import seafile_api
|
from seaserv import seafile_api
|
||||||
from pysearpc import SearpcError
|
from pysearpc import SearpcError
|
||||||
@ -147,7 +150,7 @@ class FileView(APIView):
|
|||||||
error_msg = 'Permission denied.'
|
error_msg = 'Permission denied.'
|
||||||
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
|
return api_error(status.HTTP_403_FORBIDDEN, error_msg)
|
||||||
|
|
||||||
# create file
|
# create new empty file
|
||||||
new_file_name = os.path.basename(path)
|
new_file_name = os.path.basename(path)
|
||||||
new_file_name = check_filename_with_rename(repo_id, parent_dir, new_file_name)
|
new_file_name = check_filename_with_rename(repo_id, parent_dir, new_file_name)
|
||||||
|
|
||||||
@ -158,6 +161,32 @@ class FileView(APIView):
|
|||||||
error_msg = 'Internal Server Error'
|
error_msg = 'Internal Server Error'
|
||||||
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
|
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
|
||||||
|
|
||||||
|
# update office file by template
|
||||||
|
if new_file_name.endswith('.xlsx'):
|
||||||
|
empty_file_path = os.path.join(OFFICE_TEMPLATE_ROOT, 'empty.xlsx')
|
||||||
|
elif new_file_name.endswith('.pptx'):
|
||||||
|
empty_file_path = os.path.join(OFFICE_TEMPLATE_ROOT, 'empty.pptx')
|
||||||
|
elif new_file_name.endswith('.docx'):
|
||||||
|
empty_file_path = os.path.join(OFFICE_TEMPLATE_ROOT, 'empty.docx')
|
||||||
|
else:
|
||||||
|
empty_file_path = ''
|
||||||
|
|
||||||
|
if empty_file_path:
|
||||||
|
# get file server update url
|
||||||
|
update_token = seafile_api.get_fileserver_access_token(
|
||||||
|
repo_id, 'dummy', 'update', username)
|
||||||
|
update_url = gen_file_upload_url(update_token, 'update-api')
|
||||||
|
|
||||||
|
# update file
|
||||||
|
try:
|
||||||
|
requests.post(
|
||||||
|
update_url,
|
||||||
|
data={'filename': new_file_name, 'target_file': path},
|
||||||
|
files={'file': open(empty_file_path, 'rb')}
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
|
||||||
new_file_path = posixpath.join(parent_dir, new_file_name)
|
new_file_path = posixpath.join(parent_dir, new_file_name)
|
||||||
file_info = self.get_file_info(username, repo_id, new_file_path)
|
file_info = self.get_file_info(username, repo_id, new_file_path)
|
||||||
return Response(file_info)
|
return Response(file_info)
|
||||||
|
@ -508,6 +508,9 @@ THUMBNAIL_SIZE_FOR_GRID = 192
|
|||||||
THUMBNAIL_IMAGE_SIZE_LIMIT = 20
|
THUMBNAIL_IMAGE_SIZE_LIMIT = 20
|
||||||
THUMBNAIL_IMAGE_ORIGINAL_SIZE_LIMIT = 256
|
THUMBNAIL_IMAGE_ORIGINAL_SIZE_LIMIT = 256
|
||||||
|
|
||||||
|
# template for create new office file
|
||||||
|
OFFICE_TEMPLATE_ROOT = os.path.join(MEDIA_ROOT, 'office-template')
|
||||||
|
|
||||||
#####################
|
#####################
|
||||||
# Global AddressBook #
|
# Global AddressBook #
|
||||||
#####################
|
#####################
|
||||||
|
Loading…
Reference in New Issue
Block a user