Skip to content

Instantly share code, notes, and snippets.

@sfboss
Last active September 24, 2022 05:08
Show Gist options
  • Save sfboss/4bdb025c0ab2d50b0e83653d82796923 to your computer and use it in GitHub Desktop.
Save sfboss/4bdb025c0ab2d50b0e83653d82796923 to your computer and use it in GitHub Desktop.
LatestforFilesExport
for i in $(cat ./filesOutput/logs/content_docs_ids.txt); do
firstthreechars=${i:0:3}
# poor mans way of checking if the line in our file is a contentdocumentid
if [ $firstthreechars = '069' ];then
echo 'downloading record '$i
# bunch of string parsing and querying to get what we need
thequerycontent="select ContentDocument.owner.name,ContentDocument.owner.id,linkedentityid,contentdocumentid,linkedentity.type,linkedentity.name,contentdocument.title,contentdocument.fileextension,id from contentdocumentlink where contentdocumentid = '"${i}"'"
thejson=$(sfdx force:data:soql:query -q "$thequerycontent" -u $1 --json | ./jq-win64.exe '.result.records')
theparentobject=$(echo "$thejson" | ./jq-win64.exe -r '.[0]|.LinkedEntity.Type')
subfoldersplusobject="$2"/"$theparentobject"
thefilename=$(echo "$thejson" | ./jq-win64.exe -r '.[0]|.ContentDocument.Title')
thefileext=$(echo "$thejson" | ./jq-win64.exe -r '.[0]|.ContentDocument.FileExtension')
thefullfilename="$subfoldersplusobject/$thefilename"
mkdir -p "$subfoldersplusobject"
# download the file and pass in the id, our custom file path, and the org passed into the script as the first param
filenameclean=$(echo $thefilename | sed 's/\#/_/g' | sed 's/\./_/g' | sed 's/\,/_/g' | sed 's/\\/_/g' | sed 's/\\(/_/g' | sed 's/\\)/_/g' | sed 's/\&/_/g' | sed 's/\:/_/g')
if [ -z $thefileext ]; then
fileextclean=""
lenfileext=0
else
fileextclean=$(echo $thefileext | sed 's/\#/_/g' | sed 's/\./_/g' | sed 's/\,/_/g' | sed 's/\\/_/g' | sed 's/\\(/_/g' | sed 's/\\)/_/g' | sed 's/\&/_/g' | sed 's/\:/_/g')
fi
lenfileext=${#fileextclean}
if [ -z $fileextclean ]; then
fileextclean=""
lenfileext=0
elif [ $lenfileext -gt 4 ];then
fileextclean=${fileextclean:0:4}
elif [ $lenfileext -eq 4 ];then
fileextclean=${fileextclean:0:4}
elif [ $lenfileext -lt 4 ];then
fileextclean=${fileextclean:0:$lenfileext}
fi
if [ $lenfileext -eq 0 ];then
filenamefinal = "$filenameclean"
else
filenamefinal="$filenameclean.$fileextclean"
fi
echo "original title and extension: $thefilename.$thefileext"
echo "using filename : $filenamefinal"
sfdx shane:data:file:download -i $i -o "${subfoldersplusobject}" -u $1 --filename "$filenamefinal"
# save the filename and .metadata extension as a file containing information about the file
echo "$thejson" > "$subfoldersplusobject/$filenamefinal.metadata"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment