Skip to content

Commit

Permalink
added features: --upload, --download, --delete-mxc, --file-name, --ke…
Browse files Browse the repository at this point in the history
…y-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 #3

See also matrix-nio/matrix-nio#308
  • Loading branch information
8go committed Jun 5, 2022
1 parent f616c61 commit 583a2bd
Show file tree
Hide file tree
Showing 11 changed files with 678 additions and 297 deletions.
143 changes: 91 additions & 52 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.28.0
2.29.0
Binary file removed dist/matrix-commander-2.28.0.tar.gz
Binary file not shown.
Binary file added dist/matrix-commander-2.29.0.tar.gz
Binary file not shown.
Binary file removed dist/matrix_commander-2.28.0-py3-none-any.whl
Binary file not shown.
Binary file added dist/matrix_commander-2.29.0-py3-none-any.whl
Binary file not shown.
619 changes: 418 additions & 201 deletions matrix_commander/matrix_commander.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# https://packaging.python.org/en/latest/tutorials/packaging-projects/
# https://setuptools.pypa.io/en/latest/userguide/
name = matrix-commander
version = 2.28.0
version = 2.29.0
author = 8go
description = A simple command-line Matrix client
long_description = file: PyPi-Instructions.md, README.md
Expand Down
207 changes: 165 additions & 42 deletions tests/test-upload.sh
Original file line number Diff line number Diff line change
@@ -1,45 +1,168 @@
#!/bin/bash

echo "=== Test 1: uploading and downloading a text file without encryption ==="
TMPFILE="test.txt.tmp"
mxc_key=$(matrix-commander --upload tests/test.txt --plain)
echo "$mxc_key" # has the URI, like "mxc://... None"
mxc="${mxc_key% *}" # before " "
key="${mxc_key##* }" # after " "
echo "mxc is \"$mxc\""
echo "key is \"$key\"" # None in our case, because plain-text
rm -f "$TMPFILE"
# download knows it is plain because we do not specify a key dictionary
matrix-commander --download "$mxc" --file-name "$TMPFILE"
diff "$TMPFILE" "tests/test.txt"
res="$?"
if [ "$res" == "0" ]; then
echo "SUCCESS"
echo "Let's look at the text."
cat "$TMPFILE" # look at the image
else
echo "FAILURE"
fi

echo "=== Test 2: uploading and downloading an image file with encryption ==="
mxc_key=$(matrix-commander --upload tests/test.s.png)
echo "$mxc_key" # has the URI, like "mxc://... {some key dictionary}"
mxc="${mxc_key% *}" # before " "
key="${mxc_key##* }" # after " "
echo "mxc is \"$mxc\""
echo "key is \"$key\""
rm -f "$TMPFILE"
# download knows it is encrypted because we specify a key dictionary
matrix-commander --download "$mxc" --file-name "$TMPFILE" --key-dict "$key"
diff "$TMPFILE" "tests/test.s.png"
res="$?"
if [ "$res" == "0" ]; then
echo "SUCCESS"
type eog >/dev/null 2>&1 && {
# just in case PATH is not set correctly
PATH=".:./matrix_commander:../matrix_commander:$PATH"

function test1() {
echo "=== Test 1: uploading and downloading a text file without encryption ==="
TMPFILE="test.txt.tmp"
mxc_key=$(matrix-commander --upload tests/test.txt --plain)
echo "$mxc_key" # has the URI, like "mxc://... None"
mxc="${mxc_key% *}" # before " "
key="${mxc_key##* }" # after " "
echo "mxc is \"$mxc\""
echo "key is \"$key\"" # None in our case, because plain-text
rm -f "$TMPFILE"
# download knows it is plain because we do not specify a key dictionary
matrix-commander --download "$mxc" --file-name "$TMPFILE"
diff "$TMPFILE" "tests/test.txt"
res="$?"
if [ "$res" == "0" ]; then
echo "SUCCESS"
echo "Let's look at the text."
cat "$TMPFILE" # look at the text file
else
echo "FAILURE"
fi
}

function test2() {
echo "=== Test 2: uploading and downloading an image file with encryption ==="
mxc_key=$(matrix-commander --upload tests/test.s.png)
echo "$mxc_key" # has the URI, like "mxc://... {some key dictionary}"
mxc="${mxc_key% *}" # before " "
key="${mxc_key##* }" # after " "
echo "mxc is \"$mxc\""
echo "key is \"$key\""
rm -f "$TMPFILE"
# download knows it is encrypted because we specify a key dictionary
matrix-commander --download "$mxc" --file-name "$TMPFILE" --key-dict "$key"
diff "$TMPFILE" "tests/test.s.png"
res="$?"
if [ "$res" == "0" ]; then
echo "SUCCESS"
type eog >/dev/null 2>&1 && {
echo "Let's look at the image."
eog "$TMPFILE" >/dev/null 2>&1
}
else
echo "FAILURE"
fi
rm -f "$TMPFILE"
}

function test3() {
echo "=== Test 3: uploading and downloading 2 text files without encryption ==="
N=2 # 2 files to test
TESTFILES=("tests/test.1.txt" "tests/test.2.txt")
TMPFILES=("test.1.txt.tmp" "test.2.txt.tmp")
rm -f "${TMPFILES[@]}"
mxc_keys=$(matrix-commander --upload ${TESTFILES[0]} ${TESTFILES[1]} --plain)
echo "$mxc_keys" # has the N URIs, like "mxc://... None"
MXCS=()
KEYS=()
for ii in $(seq $N); do
echo "Handling file $ii"
mxc_key=$(echo "$mxc_keys" | head -n $ii | tail -n 1)
mxc="${mxc_key% *}" # before " "
key="${mxc_key##* }" # after " "
echo "mxc is \"$mxc\""
echo "key is \"$key\"" # None in our case, because plain-text
MXCS+=("$mxc") # append mxc to array
KEYS+=("$key") # append mxc to array
done
rm -f "${TMPFILES[0]}" "${TMPFILES[1]}"
# download knows it is plain because we do not specify a key dictionary
matrix-commander --download "${MXCS[0]}" "${MXCS[1]}" \
--file-name "${TMPFILES[0]}" "${TMPFILES[1]}"
for ii in $(seq $N); do
((ii = ii - 1))
diff "${TMPFILES[$ii]}" "${TESTFILES[$ii]}"
res="$?"
if [ "$res" == "0" ]; then
echo "SUCCESS"
echo "Let's look at the text."
cat "${TMPFILES[$ii]}" # look at the text file
else
echo "FAILURE"
fi
rm "${TMPFILES[$ii]}"
done
}

function test4() {
echo "=== Test 4: convert MXC to HTTP URL ==="
TMPFILE="test.txt.tmp"
rm -f "$TMPFILE"
mxc_http=$(matrix-commander --mxc-to-http "${MXCS[0]}")
echo "$mxc_http" # has the URI and the URL
mxc="${mxc_http% *}" # before " "
http="${mxc_http##* }" # after " "
echo "mxc is \"$mxc\""
echo "mxc_http is \"$http\""
wget -O "$TMPFILE" "$http"
diff "$TMPFILE" "tests/test.1.txt"
res="$?"
if [ "$res" == "0" ]; then
echo "SUCCESS"
echo "Let's look at the image."
eog "$TMPFILE" >/dev/null 2>&1
}
else
echo "FAILURE"
fi
rm -f "$TMPFILE"
cat "$TMPFILE" # look at the text file
else
echo "FAILURE"
fi
rm -f "$TMPFILE"

}

function test5() {
echo "=== Test 5: uploading and downloading 2 text files with encryption ==="
N=2 # 2 files to test
TESTFILES=("tests/test.1.txt" "tests/test.2.txt")
TMPFILES=("test.1.txt.tmp" "test.2.txt.tmp")
rm -f "${TMPFILES[@]}"
mxc_keys=$(matrix-commander --upload ${TESTFILES[0]} ${TESTFILES[1]})
echo "$mxc_keys" # has the N URIs, like "mxc://... None"
MXCS=()
KEYS=()
for ii in $(seq $N); do
echo "Handling file $ii"
mxc_key=$(echo "$mxc_keys" | head -n $ii | tail -n 1)
mxc="${mxc_key% *}" # before " "
key="${mxc_key##* }" # after " "
echo "mxc is \"$mxc\""
echo "key is \"$key\"" # a key dict
MXCS+=("$mxc") # append mxc to array
KEYS+=("$key") # append mxc to array
done
rm -f "${TMPFILES[0]}" "${TMPFILES[1]}"
# download knows it is encrypted because we do specify a key dictionary
matrix-commander --download "${MXCS[0]}" "${MXCS[1]}" \
--file-name "${TMPFILES[0]}" "${TMPFILES[1]}" \
--key-dict "${KEYS[0]}" "${KEYS[1]}"
for ii in $(seq $N); do
((ii = ii - 1))
diff "${TMPFILES[$ii]}" "${TESTFILES[$ii]}"
res="$?"
if [ "$res" == "0" ]; then
echo "SUCCESS"
echo "Let's look at the text."
cat "${TMPFILES[$ii]}" # look at the text file
else
echo "FAILURE"
fi
rm "${TMPFILES[$ii]}"
done
}

function test6() {
echo "=== Test 6: deleting MXC resources from content repository ==="
echo "Note: This will FAIL if you do not have server admin permissions!"
matrix-commander --delete-mxc "${MXCS[0]}" "${MXCS[1]}"
}

test1
test2
test3
test4
test5
test6
1 change: 1 addition & 0 deletions tests/test.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test 1 file
1 change: 1 addition & 0 deletions tests/test.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test 2 file

0 comments on commit 583a2bd

Please sign in to comment.