Skip to content

Commit

Permalink
Refactored uploading files into items
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmajchrak committed Jan 10, 2024
1 parent 78db324 commit 726bd91
Show file tree
Hide file tree
Showing 103 changed files with 60 additions and 34 deletions.
94 changes: 60 additions & 34 deletions src/create_bitstreams/create_bitstreams.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import logging
import os
import zipfile

import src.dspace # noqa
import src.settings # noqa
import src.project_settings # noqa
import src.settings # noqa
import src.project_settings # noqa

from src.dspace.impl.models import Item
from src.utils import update_settings

env = update_settings(src.settings.env, src.project_settings.settings)

MULTIPART_CONTENT_TYPE = 'multipart/form-data'
HUNDRED_FILES_PATH = 'hundred_of_files'
ZIP_FILES_PATH = 'zip_files'
COPIES_COUNT = 100

TEMPLATE_FILE_PATH = 'template.png'
ZIP_FILE_PATH = 'zipfile.zip'
BIG_FILE_PATH = 'bigfile.txt'

COMMUNITY_2_CREATE = {
"type": {
Expand Down Expand Up @@ -59,45 +63,62 @@
"withdrawn": False,
}


def load_files_from_folder(folder_path):
def remove_file(path):
"""
Load all files from folder.
@param folder_path: path to the folder
@return: list of files or None if folder does not exist
Remove file from path.
@param path: path to the file
"""
# Check if the folder path exists
if not os.path.exists(folder_path):
logging.warning(f"The folder '{folder_path}' does not exist.")
return None

f = []
for (dirpath, dirnames, filenames) in os.walk(folder_path):
f.extend(filenames)
break
try:
os.remove(path)
except OSError as e:
logging.warning(f"Error: {e.filename} - {e.strerror}.")

return f


def create_bistreams_from_folder(dspace_client, item, folder_path):
def create_bistreams(dspace_client, item, is_bigfile=False, is_zipfile=False, is_hundred_files=False):
"""
Create a bitstream for each file from specific folder.
@param dspace_client: dspace client
Create bitstreams for item.
@param dspace_client: dsapce client
@param item: item where the bitstreams will be created
@param folder_path: folder path where the files are located
@param is_bigfile: if create an Item with big file
@param is_zipfile: if create an Item with zip file
@param is_hundred_files: if create an Item with 100 files
"""
# Create a bundle for item where the files will be uploaded
original_bundle = dspace_client.create_bundle(item)
if not original_bundle:
logging.warning(f'Bundle was not created.')

# Load files from folder
files = load_files_from_folder(folder_path)
if not files:
logging.warning(f'No files were loaded from the folder {folder_path}')
for file_name in files:
logging.info(f'Creating bitstream with file: {file_name}')
dspace_client.create_bitstream(original_bundle, file_name, f'{folder_path}/{file_name}', MULTIPART_CONTENT_TYPE)
if is_hundred_files:
for i in range(COPIES_COUNT):
# create bitstream
logging.info(f'Creating bitstream with file: template_{i}')
dspace_client.create_bitstream(original_bundle, TEMPLATE_FILE_PATH, TEMPLATE_FILE_PATH,
MULTIPART_CONTENT_TYPE)
return

if is_zipfile:
# generate zip file
zipfile.ZipFile(ZIP_FILE_PATH, mode='w').write(TEMPLATE_FILE_PATH)

# create bitstream
logging.info(f'Creating bitstream with file: {ZIP_FILE_PATH}')
dspace_client.create_bitstream(original_bundle, ZIP_FILE_PATH, ZIP_FILE_PATH, MULTIPART_CONTENT_TYPE)
remove_file(ZIP_FILE_PATH)
return

if is_bigfile:
# generate big file
with open(BIG_FILE_PATH, 'wb') as f:
# 3GB
f.seek(3 * 1024 * 1024 * 1024)
f.write(b'\0')

# create bitstream
logging.info(f'Creating bitstream with file: {BIG_FILE_PATH}')
dspace_client.create_bitstream(original_bundle, BIG_FILE_PATH, BIG_FILE_PATH,
MULTIPART_CONTENT_TYPE)
remove_file(BIG_FILE_PATH)
return


def create_item_with_title(dspace_client, parent, title):
Expand Down Expand Up @@ -131,10 +152,15 @@ def create_item_with_title(dspace_client, parent, title):
if not collection:
logging.warning(f'Collection was not created.')

# TODO Update existing item if it doesn't exist or create a new one
# Create item with 100 bitstreams
item_hundred_files = create_item_with_title(dspace_be, collection, 'Hundred Files')
create_bistreams_from_folder(dspace_be, item_hundred_files, HUNDRED_FILES_PATH)
create_bistreams(dspace_be, item_hundred_files, is_hundred_files=True)

# Create item with zip bitstream
item_zip_file = create_item_with_title(dspace_be, collection, 'Zip File')
create_bistreams_from_folder(dspace_be, item_zip_file, ZIP_FILES_PATH)
item_zip_files = create_item_with_title(dspace_be, collection, 'Zip File')
create_bistreams(dspace_be, item_zip_files, is_zipfile=True)

# Create item with big bitstream
item_big_file = create_item_with_title(dspace_be, collection, 'Big File')
create_bistreams(dspace_be, item_big_file, is_bigfile=True)
Binary file removed src/create_bitstreams/hundred_of_files/Image_1.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_10.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_100.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_11.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_12.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_13.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_14.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_15.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_16.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_17.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_18.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_19.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_2.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_20.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_21.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_22.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_23.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_24.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_25.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_26.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_27.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_28.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_29.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_3.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_30.png
Binary file not shown.
Binary file removed src/create_bitstreams/hundred_of_files/Image_31.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_32.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_33.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_34.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_35.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_36.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_37.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_38.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_39.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_4.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_40.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_41.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_42.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_43.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_44.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_45.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_46.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_47.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_48.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_49.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_5.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_50.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_51.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_52.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_53.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_54.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_55.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_56.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_57.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_58.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_59.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_6.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_60.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_61.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_62.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_63.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_64.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_65.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_66.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_67.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_68.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_69.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_7.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_70.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_71.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_72.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_73.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_74.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_75.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_76.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_77.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_78.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_79.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_8.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_80.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_81.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_82.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_83.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_84.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_85.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_86.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_87.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_88.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_89.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_9.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_90.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_91.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_92.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_93.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_94.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_95.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_96.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_97.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_98.png
Diff not rendered.
Binary file removed src/create_bitstreams/hundred_of_files/Image_99.png
Diff not rendered.
Binary file added src/create_bitstreams/template.png
Binary file removed src/create_bitstreams/zip_files/images.zip
Binary file not shown.

0 comments on commit 726bd91

Please sign in to comment.