anthony revised this gist . Go to revision
No changes
anthony revised this gist . Go to revision
1 file changed, 103 insertions
restore-orphaned-media.sh(file created)
| @@ -0,0 +1,103 @@ | |||
| 1 | + | #!/bin/bash | |
| 2 | + | ||
| 3 | + | srcPath='/mnt/Data/Фото/!_SORT/SamsungSort' | |
| 4 | + | ownerId='b3f46eb5-81a6-4283-aade-d726823d7af4' | |
| 5 | + | ||
| 6 | + | ######################################################## | |
| 7 | + | ||
| 8 | + | source .env || exit 10 | |
| 9 | + | libHostPath="$UPLOAD_LOCATION" | |
| 10 | + | libContainerPath='/usr/src/app/upload' | |
| 11 | + | ||
| 12 | + | sql="SELECT \"id\", | |
| 13 | + | \"originalPath\", | |
| 14 | + | \"originalFileName\" | |
| 15 | + | FROM asset | |
| 16 | + | WHERE 1=1 | |
| 17 | + | AND \"libraryId\" IS NULL | |
| 18 | + | AND \"status\" = 'active' | |
| 19 | + | # AND \"type\" = 'IMAGE' | |
| 20 | + | AND \"ownerId\" = '$ownerId' | |
| 21 | + | " | |
| 22 | + | echo "$sql" | |
| 23 | + | ||
| 24 | + | IFS='|' rows=($(docker exec immich_postgres psql -U "$DB_USERNAME" -d "$DB_DATABASE_NAME" -c "$sql" -t)) | |
| 25 | + | ||
| 26 | + | awk <<< ${rows[@]} " | |
| 27 | + | @load \"filefuncs\" | |
| 28 | + | function join(array, start, end, sep, result) | |
| 29 | + | { | |
| 30 | + | if (sep == \"\") { | |
| 31 | + | sep = \" \" | |
| 32 | + | } else if (sep == SUBSEP) { # magic value | |
| 33 | + | sep = \"\" | |
| 34 | + | } | |
| 35 | + | ||
| 36 | + | result = array[start] | |
| 37 | + | for (i = start + 1; i <= end; i++) { | |
| 38 | + | if (array[i] == sep) { | |
| 39 | + | continue | |
| 40 | + | } | |
| 41 | + | result = result sep array[i] | |
| 42 | + | } | |
| 43 | + | return result | |
| 44 | + | } | |
| 45 | + | { | |
| 46 | + | if (NR == 20) { | |
| 47 | + | exit | |
| 48 | + | } | |
| 49 | + | ||
| 50 | + | type=\$4 | |
| 51 | + | guid=\$1 | |
| 52 | + | print \"[\" NR \"] \" type \" \" guid | |
| 53 | + | ||
| 54 | + | immichUploadPath=\$2 | |
| 55 | + | print \"\t* Immich upload path:\t\" immichUploadPath | |
| 56 | + | ||
| 57 | + | immichUploadPath=sprintf(\"%s/%s\", \"$libHostPath\", substr(immichUploadPath, length(\"$libContainerPath\") + 2, length(immichUploadPath))) | |
| 58 | + | n=split(immichUploadPath, arr, \"/\") | |
| 59 | + | immichUploadDir=join(arr, 1, n-1, \"/\") | |
| 60 | + | immichUploadStat=stat(immichUploadPath, fdata) | |
| 61 | + | immichUploadSize=fdata[\"size\"] | |
| 62 | + | print \"\t* Immich local dir:\t\" immichUploadDir | |
| 63 | + | print \"\t* Immich local path:\t\" immichUploadPath | |
| 64 | + | print \"\t* Immich local size:\t\" immichUploadSize | |
| 65 | + | ||
| 66 | + | n=split(immichUploadPath, arr, \"/\") | |
| 67 | + | immichThumbDir=join(arr, 1, n-3, \"/\") \"/\" substr(guid, 1, 2) \"/\" substr(guid, 3, 2) | |
| 68 | + | immichThumbPath=immichThumbDir \"/\" guid \"-preview.jpeg\" | |
| 69 | + | gsub(/upload/, \"thumbs\", immichThumbPath) | |
| 70 | + | immichThumbStat=stat(immichThumbPath, fdata) | |
| 71 | + | immichThumbSize=fdata[\"size\"] | |
| 72 | + | print \"\t* Immich thumb dir:\t\" immichThumbDir | |
| 73 | + | print \"\t* Immich thumb path:\t\" immichThumbPath | |
| 74 | + | print \"\t* Immich thumb size:\t\" immichThumbSize | |
| 75 | + | ||
| 76 | + | origHostPath=sprintf(\"%s/%s\", \"$srcPath\", \$3) | |
| 77 | + | origHostStat=stat(origHostPath, fdata) | |
| 78 | + | origHostSize=fdata[\"size\"] | |
| 79 | + | print \"\t* Original host path:\t\" origHostPath | |
| 80 | + | print \"\t* Original host size:\t\" origHostSize | |
| 81 | + | ||
| 82 | + | if (origHostStat != 0 && immichUploadStat != 0 && immichThumbStat != 0) { | |
| 83 | + | print \">>> BADBADBAD\" | |
| 84 | + | } else if (origHostStat == 0) { | |
| 85 | + | if (immichUploadStat == 0) { | |
| 86 | + | print \">>> ORIG OK\" | |
| 87 | + | } else { | |
| 88 | + | system(\"mkdir -p \" immichUploadDir) | |
| 89 | + | system(\"cp -f \" origHostPath \" \" immichUploadPath) | |
| 90 | + | print \">>> ORIG COPIED\" | |
| 91 | + | } | |
| 92 | + | if (immichThumbStat == 0) { | |
| 93 | + | print \">>> THUMB OK\" | |
| 94 | + | } else { | |
| 95 | + | # system(\"mkdir -p \" immichThumbDir) | |
| 96 | + | # system(\"cp -f \" origHostPath \" \" immichThumbPath) | |
| 97 | + | # print \">>> THUMB COPIED\" | |
| 98 | + | print \">>> THUMB SKIPPED\" | |
| 99 | + | } | |
| 100 | + | } | |
| 101 | + | ||
| 102 | + | print \"\" | |
| 103 | + | }" | |
Newer
Older