Implement download functionality
- directories are served as a zip-file
This commit is contained in:
32
util.py
32
util.py
@ -1,8 +1,10 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import configparser
|
||||
import hashlib
|
||||
import os
|
||||
import variables as var
|
||||
import zipfile
|
||||
|
||||
__CONFIG = configparser.ConfigParser(interpolation=None)
|
||||
__CONFIG.read("configuration.ini", encoding='latin-1')
|
||||
@ -23,6 +25,36 @@ def get_recursive_filelist_sorted(path):
|
||||
filelist.sort()
|
||||
return filelist
|
||||
|
||||
# - zips all files of the given zippath (must be a directory)
|
||||
# - returns the absolute path of the created zip file
|
||||
# - zip file will be in the applications tmp folder (according to configuration)
|
||||
# - format of the filename itself = prefix_hash.zip
|
||||
# - prefix can be controlled by the caller
|
||||
# - hash is a sha1 of the string representation of the directories' contents (which are
|
||||
# zipped)
|
||||
def zipdir(zippath, zipname_prefix=None):
|
||||
zipname = __CONFIG.get('bot', 'tmp_folder')
|
||||
if zipname_prefix and '../' not in zipname_prefix:
|
||||
zipname += zipname_prefix.strip().replace('/', '_') + '_'
|
||||
|
||||
files = get_recursive_filelist_sorted(zippath)
|
||||
hash = hashlib.sha1((str(files).encode())).hexdigest()
|
||||
zipname += hash + '.zip'
|
||||
|
||||
if os.path.exists(zipname):
|
||||
return zipname
|
||||
|
||||
zipf = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
|
||||
|
||||
for file in files:
|
||||
filepath = os.path.dirname(file)
|
||||
file_to_add = os.path.join(zippath, file)
|
||||
add_file_as = os.path.relpath(os.path.join(zippath, file), os.path.join(zippath, '..'))
|
||||
zipf.write(file_to_add, add_file_as)
|
||||
|
||||
zipf.close()
|
||||
return zipname
|
||||
|
||||
class Dir(object):
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
Reference in New Issue
Block a user