*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit bb1f00a1 authored by Moser, Maximilian's avatar Moser, Maximilian
Browse files

Update upload.sh for InvenioRDM 1.0+

* add support for authentication tokens (required by InvenioRDM 1.0+)
* add switch to not publish created records
parent 59623039
Branches
No related tags found
1 merge request!1Updates
#!/bin/bash
# script for creating records in invenio (november 2020 release)
# script for creating records in invenio RDM 1.0+
#
# a record should either consist of a single json file containing the metadata
# in the expected format (record.json), or it should be a directory containing
......@@ -15,12 +15,26 @@
# ...
function usage () {
echo "usage: $0 [ RECORD_DIR | RECORD_JSON ] ..." 1>&2
echo "usage: $0 [ -t ACCESS_TOKEN ] [ -u BASE_URL ] [ -P ] [ RECORD_DIR | RECORD_JSON ] ..." 1>&2
echo 1>&2
echo " -t ACCESS_TOKEN the personal access token for authorization" 1>&2
echo " if not specified, the ACCESS_TOKEN environment" 1>&2
echo " variable is used" 1>&2
echo 1>&2
echo " -u BASE_URL the base URL for requests" 1>&2
echo " default: https://localhost:5000" 1>&2
echo 1>&2
echo " -P do not publish the created records" 1>&2
echo " (keep them in the draft state)" 1>&2
}
base_url="https://localhost:5000"
while getopts "u:h" opt; do
publish=1
while getopts "t:u:hP" opt; do
case "${opt}" in
t)
ACCESS_TOKEN=${OPTARG}
;;
u)
base_url=${OPTARG}
;;
......@@ -28,6 +42,9 @@ while getopts "u:h" opt; do
usage
exit 0
;;
P)
publish=0
;;
*)
usage
exit 1
......@@ -39,28 +56,31 @@ shift $(( OPTIND - 1 ))
if [[ $# -lt 1 ]]; then
usage
exit 1
elif [[ -z ${ACCESS_TOKEN} ]]; then
echo "error: no ACCESS_TOKEN was specified" 1>&2
exit 1
fi
record_dirs=()
record_files=()
while [[ $# -gt 0 ]]; do
record=$1
if [[ -f $record ]]; then
jq . $record > /dev/null
if [[ -f ${record} ]]; then
jq . ${record} > /dev/null
if [[ $? -eq 0 ]]; then
record_files+=( $record )
record_files+=( ${record} )
else
echo "not a valid json file: $record/metadata.json" 1>&2
echo "not a valid json file: ${record}/metadata.json" 1>&2
fi
elif [[ -d $record && -f $record/metadata.json ]]; then
jq . $record/metadata.json > /dev/null
elif [[ -d ${record} && -f ${record}/metadata.json ]]; then
jq . ${record}/metadata.json > /dev/null
if [[ $? -eq 0 ]]; then
record_dirs+=( $record )
record_dirs+=( ${record} )
else
echo "not a valid json file: $record/metadata.json" 1>&2
echo "not a valid json file: ${record}/metadata.json" 1>&2
fi
else
echo "unknown entity: $record" 1>&2
echo "unknown entity: ${record}" 1>&2
fi
shift
......@@ -71,45 +91,53 @@ if [[ $(( ${#record_dirs[@]} + ${#record_files[@]} )) -le 0 ]]; then
exit 1
fi
auth_header="Authorization: Bearer ${ACCESS_TOKEN}"
json_header="Content-Type: application/json"
octet_header="Content-Type: application/octet-stream"
recids=()
for record in ${record_dirs[@]}; do
recid=$(curl -sk -X POST -H 'Content-Type: application/json' -d @${record}/metadata.json ${base_url}/api/records | jq ".id")
recid=$(curl -sk -X POST -H "${json_header}" -H "${auth_header}" -d @${record}/metadata.json ${base_url}/api/records | jq ".id")
recid=${recid%\"}
recid=${recid#\"}
recids+=( $recid )
recids+=( ${recid} )
files=()
file_objects=""
if [[ -d $record/files ]]; then
for file in $record/files/*; do
if [[ ! -f $file ]]; then
echo "not a file: $file" 1>&2
if [[ -d ${record}/files ]]; then
for file in ${record}/files/*; do
if [[ ! -f ${file} ]]; then
echo "not a file: ${file}" 1>&2
continue
fi
file_name=$(basename $file)
files+=( $file_name )
file_name=$(basename ${file})
files+=( ${file_name} )
file_object="{\"key\":\"${file_name}\"}"
file_objects="${file_objects},${file_object}"
done
fi
curl -sk -X POST -H 'Content-Type: application/json' -d "[${file_objects:1}]" ${base_url}/api/records/${recid}/draft/files > /dev/null
curl -sk -X POST -H "${json_header}" -H "${auth_header}" -d "[${file_objects:1}]" ${base_url}/api/records/${recid}/draft/files > /dev/null
for file in ${files[@]}; do
curl -sk -X PUT -H 'Content-Type: application/octet-stream' --upload-file ${record}/files/${file} ${base_url}/api/records/${recid}/draft/files/${file}/content > /dev/null
curl -sk -X POST ${base_url}/api/records/${recid}/draft/files/${file}/commit > /dev/null
file_base_url=${base_url}/api/records/${recid}/draft/files/${file}
curl -sk -X PUT -H "${octet_header}" -H "${auth_header}" --upload-file ${record}/files/${file} ${file_base_url}/content > /dev/null
curl -sk -X POST -H "${auth_header}" ${file_base_url}/commit > /dev/null
done
curl -sk -X POST ${base_url}/api/records/${recid}/draft/actions/publish > /dev/null
if [[ $publish -gt 0 ]]; then
curl -sk -X POST -H "${auth_header}" ${base_url}/api/records/${recid}/draft/actions/publish > /dev/null
fi
done
for record in ${record_files[@]}; do
recid=$(curl -sk -X POST -H 'Content-Type: application/json' -d @${record} ${base_url}/api/records | jq ".id")
recid=$(curl -sk -X POST -H "${json_header}" -H "${auth_header}" -d @${record} ${base_url}/api/records | jq ".id")
recid=${recid%\"}
recid=${recid#\"}
recids+=( $recid )
recids+=( ${recid} )
curl -sk -X POST ${base_url}/api/records/${recid}/draft/actions/publish > /dev/null
if [[ $publish -gt 0 ]]; then
curl -sk -X POST -H "${auth_header}" ${base_url}/api/records/${recid}/draft/actions/publish > /dev/null
fi
done
for recid in ${recids[@]}; do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment