diff --git a/stpkg b/stpkg index 287c65033455a52bfebec59e01686f186f58197f..019cd905cd1c864bd31a1f43da3019f6d2aa3dcc 100755 --- a/stpkg +++ b/stpkg @@ -175,7 +175,7 @@ file_get_height() { file_get_type() { xdg-mime query filetype "$1"; } file_get_ext() { xdg-mime query filetype "$1" | sed 's+^.*/++'; } file_get_ext_from_type() { echo "$1" | sed 's+^.*/++'; } -file_get_name() { echo "$1" | cut -f1 -d'.'; } +file_get_name() { echo -n "$1" | cut -f1 -d'.'; } __upload_file() { local TYPE=$1 # The type of file (png/gif) @@ -225,7 +225,7 @@ create_sticker_json() { local TYPE=$5 # The type of the picture (png/gif) local MXC=$6 # The MXC URL - echo -n "{\"body\":\"$NAME\",\"info\":{\"mimetype\":\"image/$TYPE\",\"h\":$HEIGHT," \ + echo -n "{\"body\":\"$NAME\",\"info\":{\"mimetype\":\"$TYPE\",\"h\":$HEIGHT," \ "\"w\":$WIDTH,\"thumbnail_url\":\"$MXC\"},\"msgtype\":\"m.sticker\",\"url\":\"$MXC\"" \ ",\"id\":\"$PACK_NAME-$NAME\"}" } @@ -788,7 +788,8 @@ __pack() { [ ! -f $DEST_INDEX ] && die "Failed to create the index file '$DEST_INDEX'" # Populate all stickers - echo -n "{\"title\":\"$PACK_NAME\",\"id\":\"`uuidgen`\",\"stickers\":[" > $DEST_INDEX + local TEMP_JSON=`mktemp --suffix=.stpkg` + echo -n "{\"title\":\"$PACK_NAME\",\"id\":\"`uuidgen`\",\"stickers\":[" > $TEMP_JSON cd $PACK_FOLDER local TOTAL_FILES=`find . -maxdepth 1 -type f | wc -l` @@ -826,55 +827,48 @@ __pack() { skipFiles[$i]="" done + local ST_JSON="" for FILE in *; do # Add a ',' only if it's not the first in the array - echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX + echo -n "$FIRST_IN_ARRAY" >> $TEMP_JSON + local STICKER_STATUS="" + local STICKER_NAME=$(file_get_name "$FILE") - if [[ -v skipFiles[$FILE] ]]; then continue; fi + if [[ -v unchangedFiles[$FILE] ]]; then + STICKER_STATUS="Unchanged" + ST_JSON=`jq ".stickers[] | select(.body == \"$STICKER_NAME\")" < "$DEST_INDEX"` - echo "Processing new file $FILE" + elif [[ -v deletedFiles[$FILE] ]]; then + STICKER_STATUS="Deleted" + ST_JSON=`jq ".stickers[] | select(.body == \"$STICKER_NAME\")" < "$DEST_INDEX"` + #TODO: remove checksum + #TODO: Don't print "," - if [[ "`md5sum $FILE | sed -e 's/\([0-9a-f]\{32\}\) .*$/\1/'`" = "`cat $SUM_FILE | grep "$FILE" | sed -e 's/\([0-9a-f]\{32\}\) .*$/\1/'`" ]]; then - die "New file is replacing existing file, that should not be happening" + else + if [[ -v updatedFiles[$FILE] ]]; then + STICKER_STATUS="Updated" + #TODO: deleted old checksum + else + STICKER_STATUS="New" + if [[ "`md5sum $FILE | sed -e 's/\([0-9a-f]\{32\}\) .*$/\1/'`" = "$(cat $SUM_FILE | grep "$FILE" | sed -e 's/\([0-9a-f]\{32\}\) .*$/\1/')" ]]; then + die "New file is replacing existing file, that should not be happening" + fi + fi + + process_sticker_file $FILE ST_JSON + md5sum $FILE >> $SUM_FILE fi - local ST_JSON="" - process_sticker_file $FILE ST_JSON $SUM_FILE - echo "$ST_JSON" >> $DEST_INDEX + echo "$ST_JSON" >> "$TEMP_JSON" # For the report - [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height" + [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height Status" FIRST_IN_ARRAY="," - echo -e "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")' - - md5sum $FILE >> $SUM_FILE + local report="`echo -ne "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")'` $STICKER_STATUS" + echo "$report" progress_reset done | column -t - - for FILE in "${!unchangedFiles[@]}"; do - echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX - [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height" - FIRST_IN_ARRAY="," - #echo -e "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")' - echo "$FILE unchanged" - done | column -t - - for FILE in "${!updatedFiles[@]}"; do - echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX - [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height" - FIRST_IN_ARRAY="," - echo "$FILE updated" - done | column -t - - for FILE in "${!deletedFiles[@]}"; do - echo -n "$FIRST_IN_ARRAY" >> $DEST_INDEX - [ "x$FIRST_IN_ARRAY" = "x" ] && echo -e "StickerName MXC Type Width Height" - FIRST_IN_ARRAY="," - #echo -e "$ST_JSON" | jq -r '[.body, .url, .info.mimetype, .info.w, .info.h]|join(" ")' - echo "$FILE deleted, but kept in the pack" - done | column -t - # Update the sum file: # Keep deleted but kept # Remove deleted and deleted @@ -888,9 +882,9 @@ __pack() { echo -ne "# $PACK_NAME\n(no preview)" > "$DEST_FOLDER/README.md" find $DEST_FOLDER -type f \( ! -name "preview.png" -and ! -name "$PACK_NAME.json" -and ! -name '.*.checksums' \) -exec rm {} \; - echo -n "]}" >> $DEST_INDEX - cat $DEST_INDEX - cat $DEST_INDEX | jq | sponge $DEST_INDEX + echo -n "]}" >> $TEMP_JSON + cat $TEMP_JSON | jq > $DEST_INDEX + echo $TEMP_JSON info "Pack created, you can now commit it and create a MR to share it with other users" }