#!/bin/bash # This software is released into public domain. # It is provided "as is", without warranties or conditions of any kind. # Anyone is free to modify, redistribute and do anything with this software. # pomf2gomf.bash: convert pomf-style flat folders with uploads to gomf storage set -eu for req in basename sha1sum cut xxd base64 tr touch; do if ! type "$req" >/dev/null; then printf "fatal error: missing command %s\n" "$req" >&2 exit 1 fi done usage() { printf "usage: %s POMF_DIR GOMF_DIR\n" "$(basename "$0")" printf " where POMF_DIR is a flat directory with files named after their file IDs and\n" printf " extensions and GOMF_DIR is the root dir (created if necessary) of gomf-web\n" } if [ $# -ne 2 ]; then usage >&2 exit 2 fi pomf="$1" gomf="$2" if ! [ -d "$pomf" ]; then printf "directory %s does not exist\n" "$pomf" >&2 exit 1 fi mkdir -p "$gomf/upload/files" mkdir -p "$gomf/upload/ids" for file in "$pomf"/*; do name="$(basename "$file")" id="${name%%.*}" ext="${name#*.}" sha="$(sha1sum -b "$file" | cut -d' ' -f1 | xxd -r -p | base64 | tr -d = | tr '+/' '-_')" if [ "$name" = "$ext" ]; then # no ext ext= else ext=".$ext" fi while [ ${#id} -lt 3 ]; do id="_$id" done id1="${id:0:1}" # first letter id2="${id:1:2}" # second and third letter hash1="${sha:0:1}" # first letter hash2="${sha:1:2}" # second and third letter hashd="$gomf/upload/files/$hash1/$hash2/$sha" idd="$gomf/upload/ids/$id1/$id2/$id" mkdir -p "$hashd" cp -va "$file" "$hashd/file" mkdir -p "$idd" ln -vs "../../../../files/$hash1/$hash2/$sha/file" "$idd/file$ext" # relative link to $gomf/upload/ids file touch --no-dereference --reference "$file" "$idd/file$ext" done