From 4d43fc1a5be7e8b83b5ee7176b1a5b0572937788 Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Sat, 10 Mar 2018 16:09:14 +0200 Subject: [PATCH] downloaders respect folders --- code/app.py | 43 +++++++++++++++---------------------------- code/utils/utils.py | 8 +++++--- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/code/app.py b/code/app.py index 60aea46..9f9d5ee 100644 --- a/code/app.py +++ b/code/app.py @@ -195,12 +195,9 @@ def list_view(name, token = None): files = [] for file in iter_folder_files(share['path']): - fp = os.path.join(share['path'],file) - status = file_stat(fp) + status = file_stat(share['path'],file) status.update({ 'token': get_direct_token(share, file), - 'name': file, - 'url': path2url(file) }) files.append(status) # direct share links not allowed if password isnt set @@ -346,16 +343,8 @@ def script_download(name = None, token = None): if not ok: return share files = [] - for file in sorted(os.listdir(share['path'])): - fp = os.path.join(share['path'],file) - if os.path.isdir(fp): - continue - if file.startswith("."): - continue - status = file_stat(fp) - status.update({ - 'token': get_direct_token(share, file) - }) + for file in iter_folder_files(share['path']): + status = file_stat(share['path'], file) files.append(status) script = """#!/bin/bash test "$1" = "-h" && { @@ -377,6 +366,7 @@ get_file() { test -f "${FILENAME}" || WRITE=1 test "$WRITE" = "1" && { echo Downloading ${FILENAME} + mkdir -p $( dirname "$FILENAME" ) curl "${ROOTURL}download/${SHARE}/${TOKEN}/${FILENAME}" > "${FILENAME}" } || { echo Skipping ${FILENAME} @@ -390,7 +380,7 @@ get_file() { for file in files: script += 'get_file "%s"\n'%( - file['name'], + file['url'], ) return script @@ -401,13 +391,8 @@ def script_direct(name = None, token = None): if not ok: return share files = [] - for file in sorted(os.listdir(share['path'])): - fp = os.path.join(share['path'],file) - if os.path.isdir(fp): - continue - if file.startswith("."): - continue - status = file_stat(fp) + for file in iter_folder_files(share['path']): + status = file_stat(share['path'], file) status.update({ 'token': get_direct_token(share, file) }) @@ -432,6 +417,7 @@ get_file() { test -f "${FILENAME}" || WRITE=1 test "$WRITE" = "1" && { echo Downloading ${FILENAME} + mkdir -p $( dirname "$FILENAME" ) curl "${ROOTURL}direct/${SHARE}/${TOKEN}/${FILENAME}" > "${FILENAME}" } || { echo Skipping ${FILENAME} @@ -444,7 +430,7 @@ get_file() { for file in files: script += 'get_file "%s" "%s"\n'%( - file['name'], + file['url'], file['token'], ) return script @@ -550,14 +536,15 @@ def download_file(name, filename, token = None): return send_from_directory(directory=share['path'], filename=filename) -def file_versionize(filename): +def file_versionize(full_path): """ Move file to versioned with integer """ - stats = file_stat(filename) - basename, extension = os.path.splitext(stats['name']) + file_dir = os.path.dirname(full_path) + file_name = os.path.basename(full_path) + basename, extension = os.path.splitext(file_name) version = 1 while True: new_name = os.path.join( - os.path.dirname(filename), + file_dir, secure_filename("%s.v%d%s"%( basename, version, @@ -568,7 +555,7 @@ def file_versionize(filename): version += 1 else: break - os.rename(filename,new_name) + os.rename(full_path,new_name) def get_share(name, require_auth = True, token = None): diff --git a/code/utils/utils.py b/code/utils/utils.py index 0e62871..70899d7 100644 --- a/code/utils/utils.py +++ b/code/utils/utils.py @@ -12,12 +12,14 @@ def file_date_human(num): ).strftime(app.config['DATE_FORMAT']) -def file_stat(filename): - s = os.stat(filename) +def file_stat(path, filename): + full_path = os.path.join(path, filename) + s = os.stat(full_path) return { 'size': file_size_MB(s.st_size), 'mtime': file_date_human(s.st_mtime), - 'name': os.path.basename(filename) + 'name': filename, + 'url': path2url(filename) }