Extract files during import_ctf

This commit is contained in:
Kevin Chung
2017-04-26 23:58:51 -04:00
parent 8af911b745
commit c493329bf8
2 changed files with 23 additions and 1 deletions

View File

@@ -85,7 +85,7 @@ class Config(object):
The default destination is the CTFd/uploads folder. If you need Amazon S3 files
you can use the CTFd S3 plugin: https://github.com/ColdHeat/CTFd-S3-plugin
'''
UPLOAD_FOLDER = os.path.normpath('uploads')
UPLOAD_FOLDER = os.path.join(os.path.dirname(__file__), 'uploads')
'''

View File

@@ -790,3 +790,25 @@ def import_ctf(backup, segments=None, erase=False):
table.insert(entry)
else:
continue
## Extracting files
files = [f for f in backup.namelist() if f.startswith('uploads/')]
upload_folder = app.config.get('UPLOAD_FOLDER')
for f in files:
filename = f.split(os.sep, 1)
if len(filename) < 2: ## just an empty uploads directory (e.g. uploads/)
continue
filename = filename[1] ## Get the second entry in the list (the actual filename)
full_path = os.path.join(upload_folder, filename)
dirname = os.path.dirname(full_path)
## Create any parent directories for the file
if not os.path.exists(dirname):
os.makedirs(dirname)
source = backup.open(f)
target = file(full_path, "wb")
with source, target:
shutil.copyfileobj(source, target)