Showing posts with label shell curl cq file folder. Show all posts
Showing posts with label shell curl cq file folder. Show all posts

Monday, February 20, 2012

Use full script to post local folder to cq server

#!/bin/bash

#configure the user and server info
#############################################################
user="admin"
password="admin"
addr="adtest-pc2:4502"
#############################################################
#check argument
if [ "$#" -ne 2 ];then
        echo "this tool need following arguments:
1.valid upload folder or file name on this machine
2.folder name on the server.tool can create folder name on serverif folder name don't exist"
        exit
fi
if [ ! -d "$1" ] && [ ! -f "$1" ];then
        echo "$1 is not valid folder or file,please input valid upload folder or file name"
        exit
fi


sourcefolder=$1
targetproject=$2
url="http://"$addr"/content/dam/"

#create target folder
actualurl=$url""$targetproject
echo $actualurl
actualurl=${actualurl// /%20}
curl -u $user:$password -F jcr:primaryType=sling:Folder $actualurl


#tmp file
tmpfile="pqtmp.txt"
# create folder on server
len=${#sourcefolder};

find  "$sourcefolder" -type d > $tmpfile
count=`wc -l < $tmpfile`
count=`expr $count - 1`
echo "-----------total folder number is $count ----------------"
current=0
while IFS= read -r file
        do
c=${#file}
if [ "$c" -gt "$len" ]; then
current=`expr $current + 1`
echo "-----------total folder number is $count , current is  $current ----------------"
name=${file:len}
echo  "source is $name"
targetfile=$targetproject""$name
echo "target is $targetfile"
actualurl=$url""$targetfile
actualurl=${actualurl// /%20}
echo $actualurl
curl -u $user:$password -F jcr:primaryType=sling:Folder $actualurl
else
echo "root folder"
fi
        done < $tmpfile

#create file on server

find  "$sourcefolder" -type f > $tmpfile
count=`wc -l < $tmpfile`
current=0
echo "-----------total file number is $count -----------------"

while IFS= read -r file
        do
c=${#file}
current=`expr $current + 1`
        echo "-----------total folder number is $count , current is  $current ----------------"
name=${file:len}
if [ "$c" -eq "$len" ];then
name=`basename $file`
targetfile=$targetproject/""$name
echo "target is $targetfile"
else
echo  "source is $name"
                targetfile=$targetproject""$name
fi
actualurl=$url""$targetfile
actualurl=${actualurl// /%20}
echo $actualurl
                curl -u $user:$password -T "$file"  $actualurl


done < $tmpfile

rm -f $tmpfile