Repository navigation
Expand file tree
/
Copy pathsharedx.sh
More file actions
executable file
·151 lines (130 loc) · 4.45 KB
/
Copy pathsharedx.sh
File metadata and controls
executable file
·151 lines (130 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
# Notes:
# - local thumbnails are the thumbnails stored in the standard thumbnail location of the system
# - shared thumbnails are the ones placed in Shared Thumbnail Repositories
# - this script was created and tested for Thunar and Tumbler, different file-manager/thumbnailer
# combos might require some small changes regarding the exact form of the paths/uris given but
# the main logic should be the same
# - currently the script supports local files only (only 'file://' uri prefixes)
# Prerequisites
# - python3
# - realpath
# - a Thumbnailer that works as described in the Thumbnail management DBus specification
directory=$1
if [ ! -e $directory ]; then
echo "Directory not found"
exit 1
fi
if [ ! "python3 --version &> /dev/null" ]; then
echo "python3 not found"
exit 2
fi
if [ ! "realpath --version &> /dev/null" ]; then
echo "realpath not found"
exit 3
fi
if [ -z "$XDG_CACHE_HOME" ]; then
localThumbnailsRepo="$HOME/.cache/"
else
localThumbnailsRepo="$XDG_CACHE_HOME/"
fi
localThumbnailsRepo+="thumbnails/"
# create find-arguments from supported filetypes
#
args=()
# args+=(-o -name "*.jpg")
# args+=(-o -name "*.png")
# get supported file types
#
filetypes=($(dbus-send --session --print-reply --dest=org.freedesktop.thumbnails.Thumbnailer1 /org/freedesktop/thumbnails/Thumbnailer1 org.freedesktop.thumbnails.Thumbnailer1.GetSupported))
# echo ${#filetypes[@]}
let "total_entries = ${#filetypes[@]} - 8"
let "offset = $total_entries / 2"
for (( i=11; i<$offset+8; i+=2 )); do
# get uri type
type=${filetypes[i]}
type="${type:1:-1}"
if [ $type != "file" ]; then
continue
fi
# get mime
let "mime_index = $i + $offset"
mime=${filetypes[mime_index]}
mime="${mime:1:-1}"
# echo $mime
# get extensions
extensions="$(grep "$mime" /etc/mime.types)"
if [ -z "$extensions" ]; then
continue
fi
for ext in $extensions; do
if [[ $ext != *"/"* ]]; then
if [[ $ext != *":"* ]]; then
if [[ $ext != *","* ]]; then
if [[ $ext != "#" ]] && [[ $ext != "obsoleted" ]]; then
# echo $ext
args+=(-o -name "*.$ext")
fi
fi
fi
fi
done
# echo $extensions
done
unset 'args[0]'
# get supported thumbnail sizes
#
sizes=($(dbus-send --session --print-reply --dest=org.freedesktop.thumbnails.Thumbnailer1 /org/freedesktop/thumbnails/Thumbnailer1 org.freedesktop.thumbnails.Thumbnailer1.GetFlavors))
for (( i=11; i<${#sizes[@]}; i+=2 )); do
size=${sizes[i]}
# remove quotes at the beginning and end
size="${size:1:-1}"
localNormalFolder="${localThumbnailsRepo}${size}/"
# change separator to newline to iterate through filepaths correctly
IFS=$'\n'
# recursively create shared thumbnail repositories
for file in $(find "$directory" -type f \( "${args[@]}" \) ); do
filePath=$(realpath "$file")
fileDir=$(dirname "$filePath")
# skip existing shared thumbnails
#
if [[ $fileDir == *".sh_thumbnails"* ]]; then
continue
fi
# used request the creation of a local thumbnail
#
fakeUri="file://$filePath"
md5FakeUri=`echo -n "$fakeUri" | md5sum | cut -d" " -f1`
# used to copy the local thumbnail to a shared repository
#
realUri=$(python3 -c "import sys, pathlib; print(pathlib.Path(input()).resolve().as_uri())" <<< $fakeUri) # encode special chars like ' '
md5Shared=`echo -n "$(basename $realUri)" | md5sum | cut -d" " -f1`
# shared locations
#
sharedThumbnailsRepo="${fileDir}/.sh_thumbnails/"
sharedNormalFolder="${sharedThumbnailsRepo}${size}/"
sharedNormalThumbnail="${sharedNormalFolder}${md5Shared}.png"
# If a shared "Normal"-size thumbnail does not exist then create one.
#
if [ ! -e $sharedNormalThumbnail ]; then
# 1. Request the creation of a "Normal"-size thumbnail by the dbus thumbnailer.
normal="${localNormalFolder}${md5FakeUri}.png"
file_mime=$(file -b --mime-type "$filePath")
# echo $file_mime
if [ ! -e $normal ]; then
cmd="dbus-send --session --print-reply --dest=org.freedesktop.thumbnails.Thumbnailer1 /org/freedesktop/thumbnails/Thumbnailer1 org.freedesktop.thumbnails.Thumbnailer1.Queue array:string:\"$fakeUri\" array:string:\"$file_mime\" string:\"$size\" string:\"default\" uint32:0"
# echo $cmd
eval $cmd
fi
# 2. Wait until the thumbnail is created.
while [ ! -e $normal ]; do
# echo "sleep1 $normal"
$(sleep 0.05)
done
# 3. Copy that thumbnail to the proper Shared Thumbnail Repository.
$(mkdir -p $sharedNormalFolder)
$(cp $normal $sharedNormalThumbnail)
# echo "copy $normal to $sharedNormalThumbnail"
fi
done
done