From cb2464133be2490e0cca63b4c9be7353ac0a556c Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sun, 30 May 2021 13:20:12 +0000 Subject: [PATCH] Fix handling of multifile notes --- notes.sh | 10 ++++++++-- test.sh | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/notes.sh b/notes.sh index 396e830..c9c8922 100755 --- a/notes.sh +++ b/notes.sh @@ -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) diff --git a/test.sh b/test.sh index 19771ac..55a3974 100755 --- a/test.sh +++ b/test.sh @@ -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."