Skip to content

Commit 583a2bd

Browse files
author
8go
committed
added features: --upload, --download, --delete-mxc, --file-name, --key-dict, --access-token
``` < --upload UPLOAD [UPLOAD ...] < Upload one or multiple files to the content < repository. The files will be given a Matrix URI and < stored on the server. --upload allows the optional < argument --plain to skip encryption for upload. < --download DOWNLOAD [DOWNLOAD ...] < Download one or multiple files from the content < repository. You must provide one or multiple Matrix < URIs (MXCs) which are strings like this < 'mxc://example.com/SomeStrangeUriKey'. If found they < will be downloaded, decrypted, and stored in local < files. If file names are specified with --file-name < the downloads will be saved with these file names. If < --file-name is not specified the original file name < from the upload will be used. If neither specified nor < 'mxc-<mxc-id>' will be used. If a file name in --file- < name contains the placeholder __mxc_id__, it will be < replaced with the mxc-id. If a file name is specified < as empty string in --file-name, then also the name < 'mxc-<mxc-id>' will be used. By default, the upload < provided to decrypt the data. Specify one or multiple < decryption keys with --key-dict. If --key-dict is not < set, not decryption is attempted; and the data might < be stored in encrypted fashion, or might be plain-text < if the --upload skipped encryption with --plain. < --delete-mxc DELETE_MXC [DELETE_MXC ...] < Delete one or multiple objects (e.g. files) from the < content repository. You must provide one or multiple < Matrix URIs (MXC) which are strings like this < 'mxc://example.com/SomeStrangeUriKey'. Alternatively, < you can just provide the MXC id, i.e. the part after < the last slash. If found they will be deleted from the < server database. In order to delete objects one must < have server admin permissions. Having only room admin < permissions is not sufficient and it will fail. Read < https://matrix-org.github.io/synapse/latest/usage/admi < nistration/admin_api/ for learning how to set server < admin permissions on the server. Alternatively, and < optionally, one can specify an access token which has < server admin permissions with the --access-token < argument. < --file-name FILE_NAME [FILE_NAME ...] < Specify one or multiple file names for some actions. < This is an optional argument. Use this option in < combination with options like --download to specify < one or multiple file names. Ignored if used by itself < without an appropriate corresponding action. < --key-dict KEY_DICT [KEY_DICT ...] < Specify one or multiple key dictionaries for < decryption. One or multiple decryption dictionaries < are provided by the --upload action as a result. A < decryption dictionary is a string like this: "{'v': < 'v2', 'key': {'kty': 'oct', 'alg': 'A256CTR', 'ext': < True, 'k': 'somekey', 'key_ops': ['encrypt', < 'decrypt']}, 'iv': 'someiv', 'hashes': {'sha256': < 'someSHA'}}". If you have a list of key dictionaries < and want to skip one, use the empty string. < --access-token ACCESS_TOKEN < Set a custom access token for use by certain actions. < It is an optional argument. By default --access-token < is ignored and not used. It is used only by the < --delete-mxc action. ``` See also Issue 8go#3 See also matrix-nio/matrix-nio#308
1 parent f616c61 commit 583a2bd

11 files changed

+678
-297
lines changed

README.md

+91-52
Large diffs are not rendered by default.

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.28.0
1+
2.29.0

dist/matrix-commander-2.28.0.tar.gz

-121 KB
Binary file not shown.

dist/matrix-commander-2.29.0.tar.gz

125 KB
Binary file not shown.
-88.6 KB
Binary file not shown.
91.2 KB
Binary file not shown.

matrix_commander/matrix_commander.py

+418-201
Large diffs are not rendered by default.

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# https://packaging.python.org/en/latest/tutorials/packaging-projects/
33
# https://setuptools.pypa.io/en/latest/userguide/
44
name = matrix-commander
5-
version = 2.28.0
5+
version = 2.29.0
66
author = 8go
77
description = A simple command-line Matrix client
88
long_description = file: PyPi-Instructions.md, README.md

tests/test-upload.sh

+165-42
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,168 @@
11
#!/bin/bash
22

3-
echo "=== Test 1: uploading and downloading a text file without encryption ==="
4-
TMPFILE="test.txt.tmp"
5-
mxc_key=$(matrix-commander --upload tests/test.txt --plain)
6-
echo "$mxc_key" # has the URI, like "mxc://... None"
7-
mxc="${mxc_key% *}" # before " "
8-
key="${mxc_key##* }" # after " "
9-
echo "mxc is \"$mxc\""
10-
echo "key is \"$key\"" # None in our case, because plain-text
11-
rm -f "$TMPFILE"
12-
# download knows it is plain because we do not specify a key dictionary
13-
matrix-commander --download "$mxc" --file-name "$TMPFILE"
14-
diff "$TMPFILE" "tests/test.txt"
15-
res="$?"
16-
if [ "$res" == "0" ]; then
17-
echo "SUCCESS"
18-
echo "Let's look at the text."
19-
cat "$TMPFILE" # look at the image
20-
else
21-
echo "FAILURE"
22-
fi
23-
24-
echo "=== Test 2: uploading and downloading an image file with encryption ==="
25-
mxc_key=$(matrix-commander --upload tests/test.s.png)
26-
echo "$mxc_key" # has the URI, like "mxc://... {some key dictionary}"
27-
mxc="${mxc_key% *}" # before " "
28-
key="${mxc_key##* }" # after " "
29-
echo "mxc is \"$mxc\""
30-
echo "key is \"$key\""
31-
rm -f "$TMPFILE"
32-
# download knows it is encrypted because we specify a key dictionary
33-
matrix-commander --download "$mxc" --file-name "$TMPFILE" --key-dict "$key"
34-
diff "$TMPFILE" "tests/test.s.png"
35-
res="$?"
36-
if [ "$res" == "0" ]; then
37-
echo "SUCCESS"
38-
type eog >/dev/null 2>&1 && {
3+
# just in case PATH is not set correctly
4+
PATH=".:./matrix_commander:../matrix_commander:$PATH"
5+
6+
function test1() {
7+
echo "=== Test 1: uploading and downloading a text file without encryption ==="
8+
TMPFILE="test.txt.tmp"
9+
mxc_key=$(matrix-commander --upload tests/test.txt --plain)
10+
echo "$mxc_key" # has the URI, like "mxc://... None"
11+
mxc="${mxc_key% *}" # before " "
12+
key="${mxc_key##* }" # after " "
13+
echo "mxc is \"$mxc\""
14+
echo "key is \"$key\"" # None in our case, because plain-text
15+
rm -f "$TMPFILE"
16+
# download knows it is plain because we do not specify a key dictionary
17+
matrix-commander --download "$mxc" --file-name "$TMPFILE"
18+
diff "$TMPFILE" "tests/test.txt"
19+
res="$?"
20+
if [ "$res" == "0" ]; then
21+
echo "SUCCESS"
22+
echo "Let's look at the text."
23+
cat "$TMPFILE" # look at the text file
24+
else
25+
echo "FAILURE"
26+
fi
27+
}
28+
29+
function test2() {
30+
echo "=== Test 2: uploading and downloading an image file with encryption ==="
31+
mxc_key=$(matrix-commander --upload tests/test.s.png)
32+
echo "$mxc_key" # has the URI, like "mxc://... {some key dictionary}"
33+
mxc="${mxc_key% *}" # before " "
34+
key="${mxc_key##* }" # after " "
35+
echo "mxc is \"$mxc\""
36+
echo "key is \"$key\""
37+
rm -f "$TMPFILE"
38+
# download knows it is encrypted because we specify a key dictionary
39+
matrix-commander --download "$mxc" --file-name "$TMPFILE" --key-dict "$key"
40+
diff "$TMPFILE" "tests/test.s.png"
41+
res="$?"
42+
if [ "$res" == "0" ]; then
43+
echo "SUCCESS"
44+
type eog >/dev/null 2>&1 && {
45+
echo "Let's look at the image."
46+
eog "$TMPFILE" >/dev/null 2>&1
47+
}
48+
else
49+
echo "FAILURE"
50+
fi
51+
rm -f "$TMPFILE"
52+
}
53+
54+
function test3() {
55+
echo "=== Test 3: uploading and downloading 2 text files without encryption ==="
56+
N=2 # 2 files to test
57+
TESTFILES=("tests/test.1.txt" "tests/test.2.txt")
58+
TMPFILES=("test.1.txt.tmp" "test.2.txt.tmp")
59+
rm -f "${TMPFILES[@]}"
60+
mxc_keys=$(matrix-commander --upload ${TESTFILES[0]} ${TESTFILES[1]} --plain)
61+
echo "$mxc_keys" # has the N URIs, like "mxc://... None"
62+
MXCS=()
63+
KEYS=()
64+
for ii in $(seq $N); do
65+
echo "Handling file $ii"
66+
mxc_key=$(echo "$mxc_keys" | head -n $ii | tail -n 1)
67+
mxc="${mxc_key% *}" # before " "
68+
key="${mxc_key##* }" # after " "
69+
echo "mxc is \"$mxc\""
70+
echo "key is \"$key\"" # None in our case, because plain-text
71+
MXCS+=("$mxc") # append mxc to array
72+
KEYS+=("$key") # append mxc to array
73+
done
74+
rm -f "${TMPFILES[0]}" "${TMPFILES[1]}"
75+
# download knows it is plain because we do not specify a key dictionary
76+
matrix-commander --download "${MXCS[0]}" "${MXCS[1]}" \
77+
--file-name "${TMPFILES[0]}" "${TMPFILES[1]}"
78+
for ii in $(seq $N); do
79+
((ii = ii - 1))
80+
diff "${TMPFILES[$ii]}" "${TESTFILES[$ii]}"
81+
res="$?"
82+
if [ "$res" == "0" ]; then
83+
echo "SUCCESS"
84+
echo "Let's look at the text."
85+
cat "${TMPFILES[$ii]}" # look at the text file
86+
else
87+
echo "FAILURE"
88+
fi
89+
rm "${TMPFILES[$ii]}"
90+
done
91+
}
92+
93+
function test4() {
94+
echo "=== Test 4: convert MXC to HTTP URL ==="
95+
TMPFILE="test.txt.tmp"
96+
rm -f "$TMPFILE"
97+
mxc_http=$(matrix-commander --mxc-to-http "${MXCS[0]}")
98+
echo "$mxc_http" # has the URI and the URL
99+
mxc="${mxc_http% *}" # before " "
100+
http="${mxc_http##* }" # after " "
101+
echo "mxc is \"$mxc\""
102+
echo "mxc_http is \"$http\""
103+
wget -O "$TMPFILE" "$http"
104+
diff "$TMPFILE" "tests/test.1.txt"
105+
res="$?"
106+
if [ "$res" == "0" ]; then
107+
echo "SUCCESS"
39108
echo "Let's look at the image."
40-
eog "$TMPFILE" >/dev/null 2>&1
41-
}
42-
else
43-
echo "FAILURE"
44-
fi
45-
rm -f "$TMPFILE"
109+
cat "$TMPFILE" # look at the text file
110+
else
111+
echo "FAILURE"
112+
fi
113+
rm -f "$TMPFILE"
114+
115+
}
116+
117+
function test5() {
118+
echo "=== Test 5: uploading and downloading 2 text files with encryption ==="
119+
N=2 # 2 files to test
120+
TESTFILES=("tests/test.1.txt" "tests/test.2.txt")
121+
TMPFILES=("test.1.txt.tmp" "test.2.txt.tmp")
122+
rm -f "${TMPFILES[@]}"
123+
mxc_keys=$(matrix-commander --upload ${TESTFILES[0]} ${TESTFILES[1]})
124+
echo "$mxc_keys" # has the N URIs, like "mxc://... None"
125+
MXCS=()
126+
KEYS=()
127+
for ii in $(seq $N); do
128+
echo "Handling file $ii"
129+
mxc_key=$(echo "$mxc_keys" | head -n $ii | tail -n 1)
130+
mxc="${mxc_key% *}" # before " "
131+
key="${mxc_key##* }" # after " "
132+
echo "mxc is \"$mxc\""
133+
echo "key is \"$key\"" # a key dict
134+
MXCS+=("$mxc") # append mxc to array
135+
KEYS+=("$key") # append mxc to array
136+
done
137+
rm -f "${TMPFILES[0]}" "${TMPFILES[1]}"
138+
# download knows it is encrypted because we do specify a key dictionary
139+
matrix-commander --download "${MXCS[0]}" "${MXCS[1]}" \
140+
--file-name "${TMPFILES[0]}" "${TMPFILES[1]}" \
141+
--key-dict "${KEYS[0]}" "${KEYS[1]}"
142+
for ii in $(seq $N); do
143+
((ii = ii - 1))
144+
diff "${TMPFILES[$ii]}" "${TESTFILES[$ii]}"
145+
res="$?"
146+
if [ "$res" == "0" ]; then
147+
echo "SUCCESS"
148+
echo "Let's look at the text."
149+
cat "${TMPFILES[$ii]}" # look at the text file
150+
else
151+
echo "FAILURE"
152+
fi
153+
rm "${TMPFILES[$ii]}"
154+
done
155+
}
156+
157+
function test6() {
158+
echo "=== Test 6: deleting MXC resources from content repository ==="
159+
echo "Note: This will FAIL if you do not have server admin permissions!"
160+
matrix-commander --delete-mxc "${MXCS[0]}" "${MXCS[1]}"
161+
}
162+
163+
test1
164+
test2
165+
test3
166+
test4
167+
test5
168+
test6

tests/test.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test 1 file

tests/test.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test 2 file

0 commit comments

Comments
 (0)