Skip to content

Instantly share code, notes, and snippets.

@sfboss
Last active October 4, 2022 01:47
Show Gist options
  • Select an option

  • Save sfboss/13a8983521310749bf71aae81b257d1a to your computer and use it in GitHub Desktop.

Select an option

Save sfboss/13a8983521310749bf71aae81b257d1a to your computer and use it in GitHub Desktop.
latest n greatest
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 linkedentity.name != 'User' AND 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"
lenfileext=${#thefileext}
firstthreecharsext=${thefileext:0:3}
mkdir -p "$subfoldersplusobject"
if [ -z $thefileext ]; then
fileextclean=""
lenfileext=0
sfdx shane:data:file:download -i $i -o $subfoldersplusobject -u $1 --filename $i
elif [ "$thefileext" == "null" ]; then
fileextclean=""
lenfileext=0
sfdx shane:data:file:download -i $i -o $subfoldersplusobject -u $1 --filename $i
elif [ $lenfileext -gt 4 ];then
fileextclean=${thefileext:0:4}
sfdx shane:data:file:download -i $i -o $subfoldersplusobject -u $1 --filename $i.$fileextclean
elif [ $lenfileext -eq 4 ];then
fileextclean=${thefileext:0:4}
sfdx shane:data:file:download -i $i -o $subfoldersplusobject -u $1 --filename $i.$fileextclean
elif [ $lenfileext -lt 4 ];then
fileextclean=${thefileext:0:$lenfileext}
sfdx shane:data:file:download -i $i -o $subfoldersplusobject -u $1 --filename $i.$fileextclean
fi
# save the filename and .metadata extension as a file containing information about the file
echo "$thejson" > "$subfoldersplusobject/$i.metadata"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment