Skip to content

Commit 43efab6

Browse files
authored
Merge pull request #104 from seatable/handle-relative-path-for-file-uploading
Update main.py
2 parents 6673f43 + 19f9c6b commit 43efab6

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

seatable_api/main.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,23 +1003,22 @@ def download_file(self, url, save_path):
10031003
f.write(response.content)
10041004

10051005
@check_auth
1006-
def upload_bytes_file(self, name, content: bytes, relative_path=None, file_type=None, replace=False):
1006+
def upload_bytes_file(self, name, content: bytes, relative_path=None, file_type='file', replace=False):
10071007
"""
1008-
relative_path: relative path for upload, if None, default {file_type}s/{date of this month} eg: files/2020-09
10091008
file_type: if relative is None, file type must in ['image', 'file'], default 'file'
10101009
return: info dict of uploaded file
10111010
"""
1011+
if file_type not in ['image', 'file']:
1012+
raise Exception('relative or file_type invalid.')
1013+
10121014
upload_link_dict = self.get_file_upload_link()
1015+
if file_type == 'image':
1016+
relative_path = upload_link_dict['img_relative_path']
1017+
else:
1018+
relative_path = upload_link_dict['file_relative_path']
1019+
10131020
parent_dir = upload_link_dict['parent_path']
10141021
upload_link = upload_link_dict['upload_link'] + '?ret-json=1'
1015-
if not relative_path:
1016-
if file_type and file_type not in ['image', 'file']:
1017-
raise Exception('relative or file_type invalid.')
1018-
if not file_type:
1019-
file_type = 'file'
1020-
relative_path = '%ss/%s' % (file_type, str(datetime.today())[:7])
1021-
else:
1022-
relative_path = relative_path.strip('/')
10231022
response = requests.post(upload_link, data={
10241023
'parent_dir': parent_dir,
10251024
'relative_path': relative_path,
@@ -1044,25 +1043,23 @@ def upload_bytes_file(self, name, content: bytes, relative_path=None, file_type=
10441043
}
10451044

10461045
@check_auth
1047-
def upload_local_file(self, file_path, name=None, relative_path=None, file_type=None, replace=False):
1046+
def upload_local_file(self, file_path, name=None, relative_path=None, file_type='file', replace=False):
10481047
"""
1049-
relative_path: relative path for upload, if None, default {file_type}s/{date of today}, eg: files/2020-09
10501048
file_type: if relative is None, file type must in ['image', 'file'], default 'file'
10511049
return: info dict of uploaded file
10521050
"""
10531051
if file_type not in ['image', 'file']:
10541052
raise Exception('file_type invalid.')
10551053
if not name:
10561054
name = file_path.strip('/').split('/')[-1]
1057-
if not relative_path:
1058-
if file_type and file_type not in ['image', 'file']:
1059-
raise Exception('relative or file_type invalid.')
1060-
if not file_type:
1061-
file_type = 'file'
1062-
relative_path = '%ss/%s' % (file_type, str(datetime.today())[:7])
1063-
else:
1064-
relative_path = relative_path.strip('/')
1055+
if file_type not in ['image', 'file']:
1056+
raise Exception('relative or file_type invalid.')
10651057
upload_link_dict = self.get_file_upload_link()
1058+
if file_type == 'image':
1059+
relative_path = upload_link_dict['img_relative_path']
1060+
else:
1061+
relative_path = upload_link_dict['file_relative_path']
1062+
10661063
parent_dir = upload_link_dict['parent_path']
10671064
upload_link = upload_link_dict['upload_link'] + '?ret-json=1'
10681065
response = requests.post(upload_link, data={

0 commit comments

Comments
 (0)