Skip to content

Commit

Permalink
Update bulk download function
Browse files Browse the repository at this point in the history
  • Loading branch information
blagojabozinovski committed Jan 17, 2025
1 parent 4996f82 commit a7b94e2
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions ckanext/bulkdownload/views.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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/<pkg_name>/bulkdownload",
Expand Down

0 comments on commit a7b94e2

Please sign in to comment.