updates
Browse files- main.py +12 -5
- scripts/sign.sh +169 -0
main.py
CHANGED
@@ -81,13 +81,20 @@ def verify_image(fileUpload: UploadFile):
|
|
81 |
# open read and write the file into the server
|
82 |
open(fn, 'wb').write(fileUpload.file.read())
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
subprocess.check_output(
|
85 |
-
|
86 |
-
"./
|
87 |
-
"sign",
|
88 |
fileUpload.filename,
|
89 |
-
"--output",
|
90 |
-
(os.getcwd() + "/static/" + fileUpload.filename),
|
91 |
]
|
92 |
)
|
93 |
|
|
|
81 |
# open read and write the file into the server
|
82 |
open(fn, 'wb').write(fileUpload.file.read())
|
83 |
|
84 |
+
# subprocess.check_output(
|
85 |
+
# [
|
86 |
+
# "./truepic",
|
87 |
+
# "sign",
|
88 |
+
# fileUpload.filename,
|
89 |
+
# "--output",
|
90 |
+
# (os.getcwd() + "/static/" + fileUpload.filename),
|
91 |
+
# ]
|
92 |
+
# )
|
93 |
+
|
94 |
subprocess.check_output(
|
95 |
+
[
|
96 |
+
"./scripts/sign.sh",
|
|
|
97 |
fileUpload.filename,
|
|
|
|
|
98 |
]
|
99 |
)
|
100 |
|
scripts/sign.sh
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env bash
|
2 |
+
|
3 |
+
set -e
|
4 |
+
|
5 |
+
MEDIA_FILE=$(readlink -f "$1")
|
6 |
+
|
7 |
+
TRUEPIC_CLI=/home/user/app/truepic
|
8 |
+
STEG_SCRIPTS=/home/user/app/scripts/
|
9 |
+
PRIVATE_KEY=/home/user/.truepic/default/private.key
|
10 |
+
|
11 |
+
echo -n "Signing media..."
|
12 |
+
signed_no_watermark=$(mktemp).jpg
|
13 |
+
${TRUEPIC_CLI} sign $MEDIA_FILE --output ${signed_no_watermark} > /dev/null 2>&1
|
14 |
+
echo " --> ${signed_no_watermark}"
|
15 |
+
|
16 |
+
echo
|
17 |
+
echo -n "Extracting manifest..."
|
18 |
+
no_watermark_manifest=$(mktemp).bin
|
19 |
+
${TRUEPIC_CLI} manifest extract ${signed_no_watermark} --output ${no_watermark_manifest} > /dev/null 2>&1
|
20 |
+
echo " --> ${no_watermark_manifest}"
|
21 |
+
|
22 |
+
echo
|
23 |
+
echo -n "Creating watermark signature..."
|
24 |
+
verification_json=$(${TRUEPIC_CLI} verify ${signed_no_watermark})
|
25 |
+
thumbnail_hash=$(
|
26 |
+
echo "${verification_json}" | \
|
27 |
+
jq -r '.manifest_store[0].assertions."c2pa.thumbnail.claim.jpeg"[0].thumbnail_id'
|
28 |
+
)
|
29 |
+
timestamp=$(
|
30 |
+
echo "${verification_json}" | \
|
31 |
+
jq -r '.manifest_store[0].trusted_timestamp.timestamp'
|
32 |
+
)
|
33 |
+
echo -n " ${thumbnail_hash}|${timestamp} ..."
|
34 |
+
watermark_signature=$(openssl dgst -sha256 -sign ${PRIVATE_KEY} <(echo "${thumbnail_hash}|${timestamp}") | base64)
|
35 |
+
echo " ${watermark_signature}"
|
36 |
+
|
37 |
+
echo
|
38 |
+
echo -n "Uploading signed media to steg.ai..."
|
39 |
+
media_id=$(${STEG_SCRIPTS}/upload.sh ${signed_no_watermark} "image/jpeg")
|
40 |
+
echo " --> media_id=${media_id}"
|
41 |
+
rm -f ${signed_no_watermark}
|
42 |
+
|
43 |
+
echo
|
44 |
+
echo -n "Uploading manifest to steg.ai..."
|
45 |
+
manifest_id=$(${STEG_SCRIPTS}/upload.sh ${no_watermark_manifest} "application/cbor")
|
46 |
+
echo " --> media_id=${manifest_id}"
|
47 |
+
|
48 |
+
echo
|
49 |
+
echo -n "Watermarking media..."
|
50 |
+
encode_response=$(
|
51 |
+
curl -s https://api.steg.ai/encode_image_async \
|
52 |
+
-H "x-api-key: ${STEG_AI_API_KEY}" \
|
53 |
+
--data-raw '{
|
54 |
+
"media_id": "'${media_id}'",
|
55 |
+
"method": 0,
|
56 |
+
"owner": "Truepic",
|
57 |
+
"custom": "{\"manifest_id\":\"'${manifest_id}'\",\"watermark_signature\":\"'${watermark_signature}'\"}"
|
58 |
+
}'
|
59 |
+
)
|
60 |
+
request_id=$(echo "$encode_response" | jq -r '.data.request_id')
|
61 |
+
|
62 |
+
if [ -z "$request_id" ] || [ "$request_id" = "null" ]; then
|
63 |
+
echo
|
64 |
+
echo "No request_id"
|
65 |
+
rm -f ${no_watermark_manifest}
|
66 |
+
exit 1;
|
67 |
+
fi
|
68 |
+
|
69 |
+
watermark_id=$(echo "$encode_response" | jq -r '.data.encode_media_id')
|
70 |
+
|
71 |
+
status_response=""
|
72 |
+
watermarking_status=""
|
73 |
+
while [ "$watermarking_status" != "Completed." ]; do
|
74 |
+
sleep 1
|
75 |
+
echo -n ".."
|
76 |
+
status_response=$(
|
77 |
+
curl -s https://api.steg.ai/media_status?request_id=${request_id} \
|
78 |
+
-H "x-api-key: ${STEG_AI_API_KEY}"
|
79 |
+
)
|
80 |
+
watermarking_status=$(echo "${status_response}" | jq -r '.data.status')
|
81 |
+
done
|
82 |
+
|
83 |
+
download_url=$(echo "${status_response}" | jq -r '.data.media_data.media_url')
|
84 |
+
echo " --> media_id=${watermark_id}"
|
85 |
+
|
86 |
+
echo
|
87 |
+
echo -n "Downloading watermarked media..."
|
88 |
+
watermarked_image=$(mktemp).jpg
|
89 |
+
curl -s -o ${watermarked_image} "$download_url"
|
90 |
+
echo " --> ${watermarked_image}"
|
91 |
+
|
92 |
+
echo
|
93 |
+
echo -n "Re-signing the watermarked media..."
|
94 |
+
${TRUEPIC_CLI} sign ${watermarked_image} \
|
95 |
+
--ingredient-manifest-store ${no_watermark_manifest} \
|
96 |
+
--output output.jpg \
|
97 |
+
--assertions-inline '{
|
98 |
+
"assertions": [
|
99 |
+
{
|
100 |
+
"label": "c2pa.actions",
|
101 |
+
"data": {
|
102 |
+
"actions": [
|
103 |
+
{
|
104 |
+
"action": "c2pa.edited",
|
105 |
+
"when": "@now",
|
106 |
+
"softwareAgent": "steg.ai",
|
107 |
+
"parameters": {
|
108 |
+
"description": "A watermark was applied."
|
109 |
+
}
|
110 |
+
}
|
111 |
+
]
|
112 |
+
}
|
113 |
+
}
|
114 |
+
]
|
115 |
+
}' > /dev/null 2>&1
|
116 |
+
echo " --> output.jpg"
|
117 |
+
rm -f ${no_watermark_manifest}
|
118 |
+
|
119 |
+
echo
|
120 |
+
echo -n "Extracting new manifest..."
|
121 |
+
with_watermark_manifest=$(mktemp).bin
|
122 |
+
${TRUEPIC_CLI} manifest extract output.jpg --output ${with_watermark_manifest} > /dev/null 2>&1
|
123 |
+
echo " --> ${with_watermark_manifest}"
|
124 |
+
|
125 |
+
echo
|
126 |
+
echo -n "Uploading new manifest to steg.ai..."
|
127 |
+
new_manifest_id=$(${STEG_SCRIPTS}/upload.sh ${with_watermark_manifest} "application/cbor")
|
128 |
+
echo " --> media_id=${new_manifest_id}"
|
129 |
+
rm -f ${with_watermark_manifest}
|
130 |
+
|
131 |
+
echo
|
132 |
+
echo -n "Updating media with new manifest ID... "
|
133 |
+
update_result=$(
|
134 |
+
curl -s https://api.steg.ai/asset \
|
135 |
+
-X POST \
|
136 |
+
-H "x-api-key: ${STEG_AI_API_KEY}" \
|
137 |
+
--data-raw '{
|
138 |
+
"media_id" : "'${watermark_id}'",
|
139 |
+
"custom": "{\"manifest_id\":\"'${new_manifest_id}'\",\"watermark_signature\":\"'${watermark_signature}'\"}"
|
140 |
+
}'
|
141 |
+
)
|
142 |
+
|
143 |
+
echo ${update_result} | jq -r '.message'
|
144 |
+
|
145 |
+
echo
|
146 |
+
echo -n "Deleting un-watermarked image (${media_id}) from steg.ai... "
|
147 |
+
delete_result=$(
|
148 |
+
curl -s https://api.steg.ai/asset \
|
149 |
+
-X DELETE \
|
150 |
+
-H "x-api-key: ${STEG_AI_API_KEY}" \
|
151 |
+
--data-raw '{
|
152 |
+
"media_id" : "'${media_id}'"
|
153 |
+
}'
|
154 |
+
)
|
155 |
+
|
156 |
+
echo ${delete_result} | jq -r '.message'
|
157 |
+
|
158 |
+
echo
|
159 |
+
echo -n "Deleting old manifest (${manifest_id}) from steg.ai... "
|
160 |
+
delete_result=$(
|
161 |
+
curl -s https://api.steg.ai/asset \
|
162 |
+
-X DELETE \
|
163 |
+
-H "x-api-key: ${STEG_AI_API_KEY}" \
|
164 |
+
--data-raw '{
|
165 |
+
"media_id" : "'${manifest_id}'"
|
166 |
+
}'
|
167 |
+
)
|
168 |
+
|
169 |
+
echo ${delete_result} | jq -r '.message'
|