diff --git a/ckanext/bulkdownload/views.py b/ckanext/bulkdownload/views.py index 09da9a8..3b59fbf 100644 --- a/ckanext/bulkdownload/views.py +++ b/ckanext/bulkdownload/views.py @@ -1,31 +1,22 @@ -from flask import Blueprint +from flask import Blueprint, send_file from ckan.common import config -import requests +import flask import ckan.model as model -import ckan.logic as logic -import ckan.plugins.toolkit as tk -import ckan.lib.base as base -import logging import os import zipfile -from pathlib import Path from io import BytesIO -from flask import Flask, send_file, request from ckan.common import g from ckan.logic.action import get -import ckan.lib.navl.dictization_functions as dict_fns -import flask - bulkdownload = Blueprint("bulkdownload", __name__) +storage_path = config.get('ckan.storage_path') def bulk_resource_download(pkg_name): - if flask.request.method == 'GET': - print("=========get first==============") + context = { "model": model, "session": model.Session, @@ -39,18 +30,18 @@ def bulk_resource_download(pkg_name): pkg_dict = get.package_show(context, pkg_name_dict) pkg_dict['resources'][0]['url'] - ############################################################# - - for x in pkg_dict['resources']: - download_link = (x['url']) - temp_filename = download_link.split('/')[-1] - - ################################################################### - return base.render( - '/downloaded.html', { - 'pkg_name': pkg_name, - } - ) + zip_buffer = BytesIO() + with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zipf: + for x in pkg_dict['resources']: + file_path = storage_path + "/resources/" + x['id'][0:3] + "/" + x['id'][3:6] + "/" + x['id'][6:] + file_name = (x['name']) + if os.path.exists(file_path): + zipf.write(file_path, arcname=file_name) # Add file to zip + + # Ensure the pointer is at the beginning of the buffer + zip_buffer.seek(0) + + return send_file(zip_buffer, mimetype='application/zip', as_attachment=True, download_name='files.zip') bulkdownload.add_url_rule("/dataset//bulkdownload",