|
1 #!/bin/bash |
|
2 # |
|
3 # dropbox-publish.sh |
|
4 # |
|
5 # publishes a file into dropbox, conveniently managed under subfolders |
|
6 # |
|
7 |
|
8 dropboxidfile="$(dirname "$0")/dropbox-id" |
|
9 |
|
10 if [ ! -f "${dropboxidfile}" ] |
|
11 then |
|
12 echo "cannot find ${dropboxidfile}" |
|
13 echo "please create this file, it shall only contain your numeric dropbox ID as found in public links, e.g. 66055976" |
|
14 exit 1 |
|
15 fi |
|
16 |
|
17 dropboxid="$(cat ${dropboxidfile})" |
|
18 inputfile="$1" |
|
19 with_xclip="1" |
|
20 |
|
21 if [ -z "$( which xclip )" ] |
|
22 then |
|
23 echo "no xclip found, cannot copy public URL into clipboard" >/dev/stderr |
|
24 with_xclip="" |
|
25 fi |
|
26 |
|
27 if [ -z "${inputfile}" ] |
|
28 then |
|
29 echo "usage: $0 <file> [directory]" |
|
30 exit 1 |
|
31 fi |
|
32 |
|
33 if [ ! -f "${inputfile}" ] |
|
34 then |
|
35 echo "error: ${inputfile} is not a regular file" >/dev/stderr |
|
36 exit 1 |
|
37 fi |
|
38 |
|
39 year=$( date +%Y ) |
|
40 remote="https://dl.dropboxusercontent.com/u/${dropboxid}" |
|
41 local="${HOME}/Dropbox/Public" |
|
42 mime=$( file --brief --mime-type "${inputfile}" ) |
|
43 folder="" |
|
44 ext3=$( basename "${inputfile}" |tail -c 5 ) |
|
45 |
|
46 if [ -n "$2" ] |
|
47 then |
|
48 folder="$2" |
|
49 reason="of user input" |
|
50 elif [ "$( basename "${inputfile}" |head -c 10 )" = "Screenshot" ] |
|
51 then |
|
52 folder="screenshots" |
|
53 reason="filename starts with Screenshot" |
|
54 elif [ "$( echo ${mime} |head -c 6 )" = "image/" ] |
|
55 then |
|
56 folder="images" |
|
57 reason="mime type starts with image/" |
|
58 elif [ "${mime}" = "text/plain" ] |
|
59 then |
|
60 folder="text" |
|
61 reason="mime type is text/plain" |
|
62 elif [ "$( echo ${mime} |head -c 5 )" = "text/" ] |
|
63 then |
|
64 folder="code" |
|
65 reason="mime type starts with text/ but is not text/plain" |
|
66 elif [ "${ext3}" = ".wad" -o "${ext3}" = ".pk3" -o "${ext3}" = ".pk7" ] |
|
67 then |
|
68 folder="wads" |
|
69 reason="filename ends with .wad, .pk3 or .pk7" |
|
70 else |
|
71 folder="misc" |
|
72 reason="no other rule matched" |
|
73 fi |
|
74 |
|
75 echo "using folder \"${folder}\" because ${reason}" |
|
76 |
|
77 path="${year}/${folder}" |
|
78 localdir="${local}/${path}" |
|
79 mkdir -pv "${localdir}" |
|
80 num=0 |
|
81 suffix="" |
|
82 |
|
83 localbasename=$(basename "${inputfile}") |
|
84 root=${localbasename%.*} |
|
85 ext=${localbasename##*.} |
|
86 |
|
87 if [ -n "${ext}" ] |
|
88 then |
|
89 ext=".${ext}" |
|
90 fi |
|
91 |
|
92 while [ -f "${localdir}/${root}${suffix}${ext}" ] |
|
93 do |
|
94 let num+=1 |
|
95 suffix="-${num}" |
|
96 done |
|
97 |
|
98 filename="${root}${suffix}${ext}" |
|
99 cp -iv "${inputfile}" "${localdir}/${filename}" |
|
100 |
|
101 url="${remote}/${path}/$(echo "${filename}" |sed 's/ /%20/g')" |
|
102 |
|
103 if [ "${with_xclip}" ] |
|
104 then |
|
105 echo -n "${url}" |xclip |
|
106 fi |
|
107 |
|
108 echo "${url}" |