root/curl_upload.sh

Revision 77949ced433bbb86ce31f26e66dc0351c3a2dffb, 2.0 KB (checked in by Xemle <xemle@phtagr.org>, 4 months ago)

Add helper script to upload files via curl/webdav

  • Property mode set to 100755
Line 
1#!/bin/bash
2
3#
4# This script is a simple helper script to upload files to
5# phtagr with curl via phtagr's webdav interface
6#
7# Use curl_upload.sh -u YOURPHTAGR -l USERNAME *.JPG
8# E.g. ./curl_upload.sh -u http://demo.phtagr.org -l demo *.JPG
9#
10# See curl_upload.sh -h for more details
11#
12# (c) 2012 by xemle@phtagr.org
13#
14
15SCRIPT=$0
16VERBOSE=1
17
18# Set following defaults here
19#   URL of phtagr instance (without webdav part)
20URL=
21#   Default directory to upload
22DIR=$(date +%F)
23#   Username
24USER=
25#   Password
26PASS=
27
28help() {
29  echo -e "$(basename $SCRIPT) file
30\t-u, --url URL
31\t\tURL of phtagr
32\t-d, --dir DIRECTORY
33\t\tDirectory to updload files. By default it is the current date
34\t\tin YYYY-MM-DD format.
35\t-l, --login USERNAME
36\t-p, --pass PASSWORD"
37}
38
39verbose() {
40  if [ "$VERBOSE" -gt 0 ]; then
41    echo $@
42  fi
43}
44
45mkcol() {
46  local _DIR=$1
47  local _PARENT=$(dirname $_DIR)
48  if [ -n "$_PARENT" -a "$_PARENT" != "." ]; then
49    mkcol "$_PARENT"
50  fi
51  verbose "Create directory $_DIR"
52  curl -c cookie.txt --digest --user "$USER:$PASS" -X MKCOL "$URL/webdav/$_DIR"
53}
54
55upload() {
56  local _DIR=$1
57  local _FILE=$2
58  local _FILENAME=$(basename $_FILE)
59  if [ ! -e "$_FILE" ]; then
60    echo "File is missing"
61  fi
62  verbose "Upload $_FILENAME to $_DIR"
63  curl -c cookie.txt --digest --user "$USER:$PASS" -T "$_FILE" "$URL/webdav/$_DIR/$_FILENAME"
64}
65
66readpass() {
67  stty -echo
68  read -p "Password: " PASS
69  echo
70  stty echo
71}
72
73DIR=$(date +%F)
74READ=1
75while [ -n "$1" -a "$READ" -gt 0 ]; do
76  case "$1" in
77    -u|--url) URL=$2; ;;
78    -d|--dir) DIR=$2; ;;
79    -l|--login) USER=$2; ;;
80    -p|--pass) PASS=$2; ;;
81    -h) help; exit 0; ;;
82    *) READ=0; ;;
83  esac
84  if [ "$READ" -gt 0 ]; then
85    shift; shift
86  fi
87done
88
89if [ -z "$URL" ]; then
90  echo "URL is missing"; help; exit 1
91fi
92if [ -z "$DIR" ]; then
93  echo "DIR is missing"; help; exit 1
94fi
95if [ -z "$USER" ]; then
96  echo "USER is missing"; help; exit 1
97fi
98
99if [ -z "$PASS" ]; then
100  readpass
101fi
102mkcol "$DIR"
103while [ -n "$1" ]; do
104  upload "$DIR" "$1"
105  shift
106done
Note: See TracBrowser for help on using the browser.