-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathimport-csv.sh
More file actions
executable file
·91 lines (73 loc) · 2.15 KB
/
import-csv.sh
File metadata and controls
executable file
·91 lines (73 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bash
if [ "$#" -ne 4 ] && [ "$#" -ne 5 ]; then
echo "Usage: $0" '$base $cert_pem_file $cert_password [$proxy] $imports_file' >&2
echo "Example: $0" 'https://localhost:4443/ ../../../ssl/owner/cert.pem Password [https://localhost:5443/] "$pwd/imports.csv"' >&2
echo "Note: special characters such as $ need to be escaped in passwords!" >&2
exit 1
fi
base="$1"
cert_pem_file=$(realpath "$2")
cert_password="$3"
if [ -n "$5" ]; then
proxy="$4"
imports_csv="$5"
else
proxy="$base"
imports_csv="$4"
fi
titles=()
queries=()
files=()
targets=()
pwd=$(realpath "$PWD")
arr_csv=()
while IFS= read -r line
do
arr_csv+=("$line")
done < <(tail -n +2 "$imports_csv")
index=0
for row in "${arr_csv[@]}"
do
query_filename=$(echo "$row" | cut -d "," -f 1)
csv_filename=$(echo "$row" | cut -d "," -f 2)
target_path=$(echo "$row" | cut -d "," -f 3)
title=$(echo "$row" | cut -d "," -f 4)
target="${base}${target_path}"
titles+=("$title")
targets+=("$target")
# create query
# Generate query ID for fragment identifier
query_id=$(uuidgen | tr '[:upper:]' '[:lower:]')
add-construct.sh \
-b "$base" \
-f "$cert_pem_file" \
-p "$cert_password" \
--proxy "$proxy" \
--title "$title" \
--uri "#${query_id}" \
--query-file "$pwd/${query_filename}" \
"$target"
query="${target}#${query_id}"
queries+=("$query")
# Calculate file URI from SHA1 hash
# The files themselves should already be uploaded by update-folder.sh
sha1sum=$(shasum -a 1 "$pwd/${csv_filename}" | awk '{print $1}')
file="${base}uploads/${sha1sum}"
files+=("$file")
# iterate
((index++))
done
# start imports - postpone until after documents are created so we don't get concurrent updates to the triplestore
for i in "${!files[@]}"; do
printf "\n### Importing CSV from %s\n\n" "${files[$i]}"
add-csv-import.sh \
-b "$base" \
-f "$cert_pem_file" \
-p "$cert_password" \
--proxy "$proxy" \
--title "${titles[$i]}" \
--query "${queries[$i]}" \
--file "${files[$i]}" \
--delimiter "," \
"${targets[$i]}"
done