Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions bin/imports/create-query.sh → bin/add-construct.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ print_usage()
{
printf "Creates a SPARQL CONSTRUCT query.\n"
printf "\n"
printf "Usage: %s options\n" "$0"
printf "Usage: %s options TARGET_URI\n" "$0"
printf "\n"
printf "Options:\n"
printf " -f, --cert-pem-file CERT_FILE .pem file with the WebID certificate of the agent\n"
printf " -p, --cert-password CERT_PASSWORD Password of the WebID certificate\n"
printf " -b, --base BASE_URI Base URI of the application\n"
printf " --proxy PROXY_URL The host this request will be proxied through (optional)\n"
printf "\n"
printf " --title TITLE Title of the chart\n"
printf " --description DESCRIPTION Description of the chart (optional)\n"
printf " --slug STRING String that will be used as URI path segment (optional)\n"
printf " --title TITLE Title of the query\n"
printf " --description DESCRIPTION Description of the query (optional)\n"
printf " --uri URI URI of the query (optional)\n"
printf "\n"
printf " --query-file ABS_PATH Absolute path to the text file with the SPARQL query string\n"
printf " --service SERVICE_URI URI of the SPARQL service specific to this query (optional)\n"
}

hash turtle 2>/dev/null || { echo >&2 "turtle not on \$PATH. Aborting."; exit 1; }

urlencode() {
python -c 'import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], sys.argv[2]))' \
"$1" "$urlencode_safe"
}

args=()
while [[ $# -gt 0 ]]
do
Expand Down Expand Up @@ -63,8 +59,8 @@ do
shift # past argument
shift # past value
;;
--slug)
slug="$2"
--uri)
uri="$2"
shift # past argument
shift # past value
;;
Expand All @@ -73,6 +69,11 @@ do
shift # past argument
shift # past value
;;
--service)
service="$2"
shift # past argument
shift # past value
;;
*) # unknown arguments
args+=("$1") # save it in an array for later
shift # past argument
Expand All @@ -81,6 +82,8 @@ do
done
set -- "${args[@]}" # restore args

target="$1"

if [ -z "$cert_pem_file" ] ; then
print_usage
exit 1
Expand All @@ -102,43 +105,38 @@ if [ -z "$query_file" ] ; then
exit 1
fi

if [ -z "$slug" ] ; then
slug=$(uuidgen | tr '[:upper:]' '[:lower:]') # lowercase
fi
encoded_slug=$(urlencode "$slug")

container="${base}queries/"
query=$(<"$query_file") # read query string from file

target="${container}${encoded_slug}/"

args+=("-f")
args+=("$cert_pem_file")
args+=("-p")
args+=("$cert_password")
args+=("-t")
args+=("text/turtle") # content type
args+=("$target")
if [ -n "$proxy" ]; then
args+=("--proxy")
args+=("$proxy")
fi

if [ -n "$uri" ] ; then
subject="<${uri}>"
else
subject="_:subject"
fi

turtle+="@prefix ldh: <https://w3id.org/atomgraph/linkeddatahub#> .\n"
turtle+="@prefix dh: <https://www.w3.org/ns/ldt/document-hierarchy#> .\n"
turtle+="@prefix dct: <http://purl.org/dc/terms/> .\n"
turtle+="@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n"
turtle+="@prefix sp: <http://spinrdf.org/sp#> .\n"
turtle+="_:query a sp:Construct .\n"
turtle+="_:query dct:title \"${title}\" .\n"
turtle+="_:query sp:text \"\"\"${query}\"\"\" .\n"
turtle+="<${target}> a dh:Item .\n"
turtle+="<${target}> foaf:primaryTopic _:query .\n"
turtle+="<${target}> dct:title \"${title}\" .\n"
turtle+="${subject} a sp:Construct .\n"
turtle+="${subject} dct:title \"${title}\" .\n"
turtle+="${subject} sp:text \"\"\"${query}\"\"\" .\n"

if [ -n "$service" ] ; then
turtle+="${subject} ldh:service <${service}> .\n"
fi
if [ -n "$description" ] ; then
turtle+="_:query dct:description \"${description}\" .\n"
turtle+="${subject} dct:description \"${description}\" .\n"
fi

# submit Turtle doc to the server
echo -e "$turtle" | turtle --base="$target" | put.sh "${args[@]}"
echo -e "$turtle" | turtle --base="$target" | post.sh "${args[@]}"
79 changes: 11 additions & 68 deletions bin/imports/create-file.sh → bin/add-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ print_usage()
{
printf "Uploads a file.\n"
printf "\n"
printf "Usage: %s options\n" "$0"
printf "Usage: %s options TARGET_URI\n" "$0"
printf "\n"
printf "Options:\n"
printf " -f, --cert-pem-file CERT_FILE .pem file with the WebID certificate of the agent\n"
Expand All @@ -14,22 +14,14 @@ print_usage()
printf " --proxy PROXY_URL The host this request will be proxied through (optional)\n"
printf "\n"
printf " --title TITLE Title of the file\n"
printf " --container CONTAINER_URI URI of the parent container (optional)\n"
printf " --description DESCRIPTION Description of the file (optional)\n"
printf " --slug STRING String that will be used as URI path segment (optional)\n"
printf "\n"
printf " --file ABS_PATH Absolute path to the file\n"
printf " --file-content-type MEDIA_TYPE Media type of the file (optional)\n"
#printf " --file-slug STRING String that will be used as the file's URI path segment (optional)\n"
printf " --content-type MEDIA_TYPE Media type of the file (optional)\n"
}

hash curl 2>/dev/null || { echo >&2 "curl not on \$PATH. Aborting."; exit 1; }

urlencode() {
python -c 'import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], sys.argv[2]))' \
"$1" "$urlencode_safe"
}

args=()
while [[ $# -gt 0 ]]
do
Expand Down Expand Up @@ -66,28 +58,13 @@ do
shift # past argument
shift # past value
;;
--slug)
slug="$2"
shift # past argument
shift # past value
;;
--container)
container="$2"
shift # past argument
shift # past value
;;
--file)
file="$2"
shift # past argument
shift # past value
;;
--file-content-type)
file_content_type="$2"
shift # past argument
shift # past value
;;
--file-slug)
file_slug="$2"
--content-type)
content_type="$2"
shift # past argument
shift # past value
;;
Expand All @@ -99,6 +76,8 @@ do
done
set -- "${args[@]}" # restore args

target="$1"

if [ -z "$cert_pem_file" ] ; then
print_usage
exit 1
Expand All @@ -119,50 +98,23 @@ if [ -z "$file" ] ; then
print_usage
exit 1
fi
if [ -z "$file_content_type" ] ; then
if [ -z "$content_type" ] ; then
# determine content-type if not provided
file_content_type=$(file -b --mime-type "$file")
content_type=$(file -b --mime-type "$file")
fi

if [ -z "$slug" ] ; then
slug=$(uuidgen | tr '[:upper:]' '[:lower:]') # lowercase
fi
encoded_slug=$(urlencode "$slug")

# need to create explicit file URI since that is what this script returns (not the graph URI)

#if [ -z "$file_slug" ] ; then
# file_slug=$(uuidgen | tr '[:upper:]' '[:lower:]') # lowercase
#fi

if [ -z "$container" ] ; then
container="${base}files/"
fi

target="${container}${encoded_slug}/"

# https://stackoverflow.com/questions/19116016/what-is-the-right-way-to-post-multipart-form-data-using-curl

rdf_post+="-F \"rdf=\"\n"
rdf_post+="-F \"sb=file\"\n"
rdf_post+="-F \"pu=http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName\"\n"
rdf_post+="-F \"ol=@${file};type=${file_content_type}\"\n"
rdf_post+="-F \"ol=@${file};type=${content_type}\"\n"
rdf_post+="-F \"pu=http://purl.org/dc/terms/title\"\n"
rdf_post+="-F \"ol=${title}\"\n"
rdf_post+="-F \"pu=http://www.w3.org/1999/02/22-rdf-syntax-ns#type\"\n"
rdf_post+="-F \"ou=http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#FileDataObject\"\n"
rdf_post+="-F \"su=${target}\"\n"
rdf_post+="-F \"pu=http://purl.org/dc/terms/title\"\n"
rdf_post+="-F \"ol=${title}\"\n"
rdf_post+="-F \"pu=http://www.w3.org/1999/02/22-rdf-syntax-ns#type\"\n"
rdf_post+="-F \"ou=https://www.w3.org/ns/ldt/document-hierarchy#Item\"\n"
rdf_post+="-F \"pu=http://xmlns.com/foaf/0.1/primaryTopic\"\n"
rdf_post+="-F \"ob=file\"\n"
rdf_post+="-F \"pu=http://rdfs.org/sioc/ns#has_container\"\n"
rdf_post+="-F \"ou=${container}\"\n"

if [ -n "$description" ] ; then
rdf_post+="-F \"sb=file\"\n"
rdf_post+="-F \"pu=http://purl.org/dc/terms/description\"\n"
rdf_post+="-F \"ol=${description}\"\n"
fi
Expand All @@ -176,14 +128,5 @@ if [ -n "$proxy" ]; then
target="${target/$target_host/$proxy_host}"
fi

# POST RDF/POST multipart form and capture the effective URL
effective_url=$(echo -e "$rdf_post" | curl -w '%{url_effective}' -f -v -s -k -X PUT -H "Accept: text/turtle" -E "$cert_pem_file":"$cert_password" -o /dev/null --config - "$target")

# If using proxy, rewrite the effective URL back to original hostname
if [ -n "$proxy" ]; then
# Replace proxy host with original host in the effective URL
rewritten_url="${effective_url/$proxy_host/$target_host}"
echo "$rewritten_url"
else
echo "$effective_url"
fi
# POST RDF/POST multipart form
echo -e "$rdf_post" | curl -f -v -s -k -X POST -H "Accept: text/turtle" -E "$cert_pem_file":"$cert_password" -o /dev/null --config - "$target"
71 changes: 71 additions & 0 deletions bin/delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash

print_usage()
{
printf "Deletes an RDF document.\n"
printf "\n"
printf "Usage: %s options TARGET_URI\n" "$0"
printf "\n"
printf "Options:\n"
printf " -f, --cert-pem-file CERT_FILE .pem file with the WebID certificate of the agent\n"
printf " -p, --cert-password CERT_PASSWORD Password of the WebID certificate\n"
printf " --proxy PROXY_URL The host this request will be proxied through (optional)\n"
}

hash curl 2>/dev/null || { echo >&2 "curl not on \$PATH. Aborting."; exit 1; }

unknown=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
-f|--cert-pem-file)
cert_pem_file="$2"
shift # past argument
shift # past value
;;
-p|--cert-password)
cert_password="$2"
shift # past argument
shift # past value
;;
--proxy)
proxy="$2"
shift # past argument
shift # past value
;;
*) # unknown option
unknown+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${unknown[@]}" # restore args

if [ -z "$cert_pem_file" ] ; then
print_usage
exit 1
fi
if [ -z "$cert_password" ] ; then
print_usage
exit 1
fi
if [ "$#" -ne 1 ]; then
print_usage
exit 1
fi

url="$1"

if [ -n "$proxy" ]; then
# rewrite target hostname to proxy hostname
url_host=$(echo "$url" | cut -d '/' -f 1,2,3)
proxy_host=$(echo "$proxy" | cut -d '/' -f 1,2,3)
final_url="${url/$url_host/$proxy_host}"
else
final_url="$url"
fi

# DELETE the document
curl -f -v -k -E "$cert_pem_file":"$cert_password" -X DELETE -o /dev/null "$final_url"
Loading