Unverified Commit 06bc2197 authored by BaiJiangJie's avatar BaiJiangJie Committed by GitHub

Merge pull request #268 from jumpserver/master

Merge from master
parents 0f03bb50 f0c63d9b
...@@ -273,7 +273,7 @@ class ElFinderConnector: ...@@ -273,7 +273,7 @@ class ElFinderConnector:
upload_paths = self.data.get('upload_path[]') upload_paths = self.data.get('upload_path[]')
if self.data.get('chunk') and self.data.get('cid'): if self.data.get('chunk') and self.data.get('cid'):
self.response.update( self.response.update(
volume.upload_as_chunk( volume.upload_as_chunk(self.data.get('cid'),
self.request.files, self.data.get('chunk'), parent, upload_paths self.request.files, self.data.get('chunk'), parent, upload_paths
) )
) )
......
...@@ -14,6 +14,7 @@ class BaseVolume: ...@@ -14,6 +14,7 @@ class BaseVolume:
self.path_sep = '/' self.path_sep = '/'
self.dir_mode = '0o755' self.dir_mode = '0o755'
self.file_mode = '0o644' self.file_mode = '0o644'
# #
# @classmethod # @classmethod
# def get_volume(cls, request): # def get_volume(cls, request):
...@@ -226,7 +227,7 @@ class BaseVolume: ...@@ -226,7 +227,7 @@ class BaseVolume:
""" """
raise NotImplementedError raise NotImplementedError
def upload_as_chunk(self, files, chunk_name, parent, upload_path): def upload_as_chunk(self, cid, files, chunk_name, parent, upload_path):
""" """
Upload a large file as chunk Upload a large file as chunk
:param files: :param files:
......
...@@ -17,10 +17,13 @@ class SFTPVolume(BaseVolume): ...@@ -17,10 +17,13 @@ class SFTPVolume(BaseVolume):
self.sftp = sftp self.sftp = sftp
self.root_name = 'Home' self.root_name = 'Home'
self._stat_cache = {} self._stat_cache = {}
self._fd_cache = dict()
self.lock = threading.Lock() self.lock = threading.Lock()
super(SFTPVolume, self).__init__() super(SFTPVolume, self).__init__()
def close(self): def close(self):
for fd in self._fd_cache.values():
fd.close()
self.sftp.close() self.sftp.close()
def get_volume_id(self): def get_volume_id(self):
...@@ -250,7 +253,7 @@ class SFTPVolume(BaseVolume): ...@@ -250,7 +253,7 @@ class SFTPVolume(BaseVolume):
added.append(self._info(path)) added.append(self._info(path))
return {'added': added} return {'added': added}
def upload_as_chunk(self, files, chunk_name, parent, upload_path): def upload_as_chunk(self, cid, files, chunk_name, parent, upload_path):
added = [] added = []
parent_path = self._path(parent) parent_path = self._path(parent)
item = files.get('upload[]') item = files.get('upload[]')
...@@ -258,8 +261,10 @@ class SFTPVolume(BaseVolume): ...@@ -258,8 +261,10 @@ class SFTPVolume(BaseVolume):
filename = '.'.join(__tmp[:-2]) filename = '.'.join(__tmp[:-2])
num, total = __tmp[-2].split('_') num, total = __tmp[-2].split('_')
num, total = int(num), int(total) num, total = int(num), int(total)
if len(upload_path) == 1 and (parent != upload_path[0]) and (filename in upload_path[0]): if upload_path and len(upload_path) == 1 and (filename in upload_path[0]):
path = self._join(parent_path, upload_path[0].lstrip(self.path_sep)) path = self._join(parent_path, upload_path[0].lstrip(self.path_sep))
elif upload_path and parent == upload_path[0]:
path = self._join(parent_path, filename)
else: else:
path = self._join(parent_path, upload_path[0].lstrip(self.path_sep), filename) path = self._join(parent_path, upload_path[0].lstrip(self.path_sep), filename)
remote_path = self._remote_path(path) remote_path = self._remote_path(path)
...@@ -268,12 +273,19 @@ class SFTPVolume(BaseVolume): ...@@ -268,12 +273,19 @@ class SFTPVolume(BaseVolume):
files_exist = [d['name'] for d in infos] files_exist = [d['name'] for d in infos]
if item.filename in files_exist: if item.filename in files_exist:
raise OSError("File {} exits".format(remote_path)) raise OSError("File {} exits".format(remote_path))
with self.sftp.open(remote_path, 'a') as rf: if cid not in self._fd_cache:
rf = self.sftp.open(remote_path, "a")
self._fd_cache[cid] = rf
else:
rf = self._fd_cache.get(cid)
for data in item: for data in item:
rf.write(data) rf.write(data)
if num != total: if num != total:
return {'added': added} return {'added': added}
else: else:
rf.close()
self._fd_cache.pop(cid)
return {'added': added, '_chunkmerged': filename, '_name': filename} return {'added': added, '_chunkmerged': filename, '_name': filename}
def upload_chunk_merge(self, parent, chunk, upload_path): def upload_chunk_merge(self, parent, chunk, upload_path):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment