Fix handling of multifile notes

This commit is contained in:
Konstantin Nazarov 2021-05-30 13:20:12 +00:00
parent 2cd09b260d
commit cb2464133b
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 40 additions and 6 deletions

View file

@ -184,7 +184,7 @@ unpack_part() {
if [[ $DISPOSITION == *"attachment"* ]]; then
ENCODING=$(get_header "$FILE" Content-Transfer-Encoding)
FILENAME=$(echo "$DISPOSITION" | \
sed -n 's/^.*filename="\{0,1\}\(.*\)"\{0,1\}$/\1/p')
sed -n 's/^.*filename="\{0,1\}\([^"]*\)"\{0,1\}$/\1/p')
FILTER="\
{ \
if (body==1) {print \$0}\
@ -289,7 +289,7 @@ pack_mime() {
{
echo "MIME-Version: 1.0"
echo "Date: $MIME_TIMESTAMP"
echo "Content-Type: multipart/mixed; boundary=$BOUNDARY"
echo "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\""
get_headers "$DIR/note.md"
echo
echo "--$BOUNDARY"
@ -346,7 +346,10 @@ new_entry() {
get_body "$INP/note.md"
} >> "$DIR/note.md"
elif [ -t 0 ]; then
OLD_DIR="$(pwd)"
cd "$DIR"
"$EDITOR" "$ENTRY_FILE"
cd "$OLD_DIR"
else
while read -r line ; do
echo "$line" >> "$ENTRY_FILE"
@ -403,9 +406,12 @@ edit_entry() {
unpack_mime "$FILENAME" "$DIR"
fi
OLD_DIR="$(pwd)"
cd "$DIR"
if ! "$EDITOR" "$DIR/note.md"; then
die "Editor returned non-zero exit code. Leaving the note untouched."
fi
cd "$OLD_DIR"
UNIX_TIMESTAMP=$(date "+%s")
HOSTNAME=$(hostname -s)

36
test.sh
View file

@ -222,7 +222,7 @@ pack_multipart() {
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header"
assert 'echo "$OUTPUT" | grep -o "text attachment"' "text attachment"
BOUNDARY="$(cat "$(pwd)/notes/cur"/* | grep boundary= | cut -d '=' -f 2)"
BOUNDARY="$(cat "$(pwd)/notes/cur"/* | grep boundary= | cut -d '=' -f 2 | tr -d '\"')"
#echo "boundary: $BOUNDARY"
OUTPUT="$(echo "$OUTPUT" | sed "s/$BOUNDARY/boundary/g")"
@ -230,7 +230,7 @@ pack_multipart() {
read -d '' -r EXPECTED <<- EOF || true
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=boundary
Content-Type: multipart/mixed; boundary="boundary"
Subject: This is a header
--boundary
@ -264,7 +264,7 @@ pack_multipart_binary() {
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header"
BOUNDARY="$(cat "$(pwd)/notes/cur"/* | grep boundary= | cut -d '=' -f 2)"
BOUNDARY="$(cat "$(pwd)/notes/cur"/* | grep boundary= | cut -d '=' -f 2 | tr -d '\"')"
#echo "boundary: $BOUNDARY"
OUTPUT="$(echo "$OUTPUT" | sed "s/$BOUNDARY/boundary/g")"
@ -272,7 +272,7 @@ pack_multipart_binary() {
read -d '' -r EXPECTED <<- EOF || true
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=boundary
Content-Type: multipart/mixed; boundary="boundary"
Subject: This is a header
--boundary
@ -343,6 +343,33 @@ no_headers_dir() {
assert 'echo "$OUTPUT" | grep "This is a body"' "This is a body"
}
import_export() {
mkdir "$TMP/inpdir"
NOTE_ID="6e50650a-88d1-49a3-92eb-0ec329e6f6f8"
DATE="2021-05-29T18:47:34Z"
cat > "$TMP/inpdir/note.md" <<- EOF
X-Date: $DATE
X-Note-Id: $NOTE_ID
Subject: This is a subject
This is a body
EOF
cat > "$TMP/inpdir/file.txt" <<- EOF
This is a text attachment
EOF
"$BASE_DIR/notes.sh" -n "$TMP/inpdir"
mkdir "$TMP/outpdir"
"$BASE_DIR/notes.sh" -E $NOTE_ID "$TMP/outpdir"
assert 'cat "$TMP/outpdir/note.md"' "$(cat "$TMP/inpdir/note.md")"
assert 'cat "$TMP/outpdir/file.txt"' "$(cat "$TMP/inpdir/file.txt")"
}
testcase new_note_from_stdin
testcase new_note_from_file
testcase new_note_from_dir
@ -355,6 +382,7 @@ testcase pack_multipart_binary
testcase existing_headers
testcase no_headers
testcase no_headers_dir
testcase import_export
if [[ "$RESULT" == "0" ]]; then
echo "All tests passed."