Import document with bash script using REST

Simplified version of the script which make use of RESTful API.

OKM_SERVICE="http://localhost:8080/openkm"
OKM_USER="okmAdmin"
OKM_PASSWORD="admin"
OKM_UUID="754aefd5-c11c-4954-a94e-b062cf3fed97"
OKM_PATH="/okm:root/upload"
LOCAL_PATH="."
TMP=$(mktemp)
RESET='\e[0m'
ERROR='\e[1;31m'
OK='\e[1;34m'
 
for DIR in $(find $LOCAL_PATH -type d); do
  DIR=${DIR:1:${#DIR}}
  if [ -n "$DIR" ]; then
    RETCODE=$(curl -u $OKM_USER:$OKM_PASSWORD --write-out %{http_code} -X POST --silent --output $TMP \
      -H "Content-Type: application/json" -d "$DIR" "$OKM_SERVICE/rest/folders/parents/$OKM_UUID")
    if [ $RETCODE != "200" ]; then
      echo -e "${ERROR}ERROR -> $(cat $TMP)${RESET}";
    else
      echo -e "${OK}Folder created: ${OKM_PATH}${DIR}${RESET}"
    fi
  fi
done
 
for FILE in $(find $LOCAL_PATH -type f); do
  FILE=${FILE:2:${#FILE}}
  RETCODE=$(curl -u $OKM_USER:$OKM_PASSWORD --write-out %{http_code} -X POST --silent --output $TMP \
    -F "docPath=$OKM_PATH/$FILE" -F content=@$FILE "$OKM_SERVICE/rest/documents/parents/$OKM_UUID")
  if [ $RETCODE != "200" ]; then
    echo -e "${ERROR}ERROR -> $(cat $TMP)${RESET}";
  else
    echo -e "${OK}Document created: ${OKM_PATH}/${FILE}${RESET}"
  fi
done
 
# Cleanup
rm -f $TMP
Table of contents [ Hide Show ]