diff --git a/media/office-template/empty.docx b/media/office-template/empty.docx new file mode 100644 index 0000000000..459ac47644 Binary files /dev/null and b/media/office-template/empty.docx differ diff --git a/media/office-template/empty.pptx b/media/office-template/empty.pptx new file mode 100644 index 0000000000..08858f0a57 Binary files /dev/null and b/media/office-template/empty.pptx differ diff --git a/media/office-template/empty.xlsx b/media/office-template/empty.xlsx new file mode 100644 index 0000000000..99aa0b2cad Binary files /dev/null and b/media/office-template/empty.xlsx differ diff --git a/seahub/api2/endpoints/file.py b/seahub/api2/endpoints/file.py index 28af56ee9d..ccf8f76fc4 100644 --- a/seahub/api2/endpoints/file.py +++ b/seahub/api2/endpoints/file.py @@ -2,6 +2,7 @@ import os import logging import posixpath +import requests from rest_framework.authentication import SessionAuthentication 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.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.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 pysearpc import SearpcError @@ -147,7 +150,7 @@ class FileView(APIView): error_msg = 'Permission denied.' 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 = check_filename_with_rename(repo_id, parent_dir, new_file_name) @@ -158,6 +161,32 @@ class FileView(APIView): error_msg = 'Internal Server Error' 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) file_info = self.get_file_info(username, repo_id, new_file_path) return Response(file_info) diff --git a/seahub/settings.py b/seahub/settings.py index 2469bc0fe5..3bf994d5f3 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -508,6 +508,9 @@ THUMBNAIL_SIZE_FOR_GRID = 192 THUMBNAIL_IMAGE_SIZE_LIMIT = 20 THUMBNAIL_IMAGE_ORIGINAL_SIZE_LIMIT = 256 +# template for create new office file +OFFICE_TEMPLATE_ROOT = os.path.join(MEDIA_ROOT, 'office-template') + ##################### # Global AddressBook # #####################