File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+
3+ set -e
4+
5+ # 注意不要添加最后的斜线
6+ API_URL=" https://example.com"
7+ AUTH=" xxxxx"
8+
9+ case " $1 " in
10+ upload)
11+ # pelit upload /some/local/file.txt directory_on_server
12+ link_re=' ^(http|https)://([^/]+)(/.*)?$'
13+ if [[ " $2 " =~ $link_re ]]; then
14+ new_filename=" $( openssl rand -hex 16) "
15+ # 从 URL 中取出 basename
16+ original_filename=" $( basename " ${BASH_REMATCH[3]} " ) "
17+ extension=" ${original_filename##* .} "
18+ location=" /tmp/pelit_${new_filename} .${extension} "
19+ curl -LsS " $2 " -o " ${location} "
20+ else
21+ location=" $2 "
22+ fi
23+
24+ # 执行上传
25+ resp=" $( curl -sS -X POST \
26+ -F " file=@${location} " \
27+ -H " Authorization: ${AUTH} " \
28+ " ${API_URL} /upload/$3 " ) "
29+
30+ # 提取返回 JSON 中的 url
31+ echo " $resp " | jq -r ' .url'
32+ ;;
33+
34+ delete)
35+ # pelit delete dir_on_server/file.txt
36+ curl -sS -X DELETE \
37+ -H " Authorization: ${AUTH} " \
38+ " ${API_URL} /delete/$2 "
39+ ;;
40+
41+ list)
42+ # pelit list {dir_on_server, /}
43+ if [ " $2 " = " /" ]; then
44+ path=" "
45+ else
46+ path=" /$2 "
47+ fi
48+ curl -sS -X GET \
49+ -H " Authorization: ${AUTH} " \
50+ " ${API_URL} /list${path} "
51+ ;;
52+
53+ * )
54+ echo " 未知命令"
55+ exit 1
56+ ;;
57+ esac
You can’t perform that action at this time.
0 commit comments