def export_folder(self, folder, directory_settings=None, filter=None): # get the directory content # relative_path = os.path.join( # self._request.registry.settings['root_dir'], # self._request.matchdict['dir']) self._folder = folder relative_path = folder listing = os.listdir(relative_path) relative_path = str(os.path.abspath(relative_path)).encode('string-escape') relative_path = relative_path.decode('string-escape') # filter the folder content itemgrouper = ItemGrouper() visible_items_by_extension, visibleitems, invisibleitems = itemgrouper.group_folder(listing, directory_settings) # filter the specific file extension if filter is not None: if filter in visible_items_by_extension: visible_items_by_extension = visible_items_by_extension[filter] output = '' if os.path.exists(relative_path + '/.intro.md'): with open(relative_path + '/.intro.md') as file: output += file.read() # iterate through the file output += self._iterate_folder('#', visible_items_by_extension) if os.path.exists(relative_path + '/.outro.md'): with open(relative_path + '/.outro.md') as file: output += file.read() return output
def handle_request(request, relative_path, directory_settings): depth = 0 listing = os.listdir(relative_path) relative_path = str(os.path.abspath(relative_path)).encode('string-escape') relative_path = relative_path.decode('string-escape') returnfilename = os.path.basename(relative_path) # filter the folder content itemgrouper = ItemGrouper() items_dict, visibleitems, invisibleitems = itemgrouper.group_folder(listing, directory_settings) if 'specific' in dict(request.params): specific_filetype = request.params['specific'] if specific_filetype in items_dict: items_dict = items_dict[specific_filetype] returnfilename += '_' + specific_filetype depth = 1 # TODO: Sometimes zip file creation doesnt halt. It seems to be dependent on the fact whether another request.zip file already exists nosubfolderallowed = 'key_not_as_folder_separator' in dict(request.params) tuples = DirectoryZipHandler._file_tuples(depth, '', items_dict) tempdirpath = tempfile.mkdtemp() zippath = tempdirpath + '/request.zip' with ZipFile(zippath, 'w') as zip: for tuple in tuples: localfilepath = relative_path + '/' + tuple[0] if tuple[0].startswith('.'): continue if os.path.isdir(localfilepath): continue if nosubfolderallowed: zip.write(localfilepath, tuple[0]) else: zip.write(localfilepath, tuple[1]) with open(zippath, 'rb') as zip: return Response(body=zip.read(), content_type='application/zip', content_disposition='attachment; filename="{0}.zip"'.format(returnfilename))