File dv-upload.sh of Package dvswitch-git

176
 
1
#!/bin/sh
2
3
# Script to easily upload videos to nue suse streaming server
4
# Kilian Petsch <kpetsch@suse.de>
5
#
6
# 
7
# 2013-01-31, V0.2  jw -- made it work also without metafile.txt
8
9
version=0.2
10
11
FILENAME=''
12
DIRECTORY=''
13
SERVER='ftp@streaming.nue.suse.com'
14
URL='http://streaming.nue.suse.com'
15
METAFILENAME=''
16
ALLFILE=''
17
LIST=''
18
OVERWRITE=''
19
20
usage () 
21
{
22
# Usage section
23
# Explain commands etc.
24
echo "      
25
      This script allows for easy uploading of videos to the SUSE streaming server.
26
      It automatically checks if the filename has the correct format, if it's
27
      already uploaded and if the upload was successful.
28
29
      Usage:
30
      dv-upload [OPTIONS] [FILE]
31
      
32
      Options:
33
        -d,--directory: Specify the directory you want to upload to.
34
                        Default upload directory is upload/.
35
        -l,--list: List directories on the server. Use '-d' to specify which one.
36
        -k,--key: Uses ssh-copy-id to authorize your key with the server.
37
                  This only works if you already have a rsa-key:
38
                  Use 'ssh-keygen -t rsa' to generate one.
39
        -o,--overwrite: Allow overwrite of existing files. Default: abort.
40
        -h,--help: Display this help.
41
42
      [FILE]
43
        Specifiy the file you want to upload. Filename needs to contain a date.
44
        If you want to add a metadata file, make sure it is in the same directory,
45
        has the same name and the file extension '.txt'
46
47
48
"
49
}
50
51
file_on_server () 
52
{
53
54
# Head request via curl to check if the file is already on the server
55
filecheck=$(mktemp /tmp/filecheck_XXXXXX.txt)
56
curl -sI "$URL/$DIRECTORY/$FILENAME" > $filecheck
57
58
if [[ $? = '0' ]]; then
59
   # Check the file for 404 Error
60
   grep -q '404 Not Found' $filecheck
61
   if [[ $? != '0' ]]; then
62
       rm $filecheck
63
       echo "File is on server"
64
       echo "$URL/$DIRECTORY/$FILENAME"
65
       if [[ -n $1 ]]; then
66
         if [[ -z $OVERWRITE ]]; then
67
           echo
68
           echo "Use -o to overwrite"
69
           exit
70
         else
71
           echo "overwriting ..."
72
           echo
73
         fi
74
       fi
75
   else
76
       rm $filecheck
77
       echo "File is not on server"
78
   fi
79
else
80
    echo "Error server not found"
81
    rm $filecheck
82
    exit 2
83
fi
84
}
85
86
servername ()
87
{
88
echo $SERVER
89
}
90
91
ARGS=`getopt -o "d:hlko" -l "directory:,help,list,key,overwrite" -n "$ME" -- "$@"`
92
93
eval set -- "$ARGS"
94
while true;
95
do
96
    case "$1" in
97
    -h|--help)
98
        usage
99
        exit 0;;
100
    -d|--directory)
101
        DIRECTORY="$2"
102
        shift 2;;
103
    -l|--list)
104
        LIST=1
105
        shift
106
        ;;
107
    -o|--overwrite)
108
        OVERWRITE=1
109
        shift
110
        ;;
111
    -k|--key)
112
            # check if there is a key available 
113
        if [[ -e ~/.ssh/id_rsa ]]; then
114
        servername
115
        ssh-copy-id -i ~/.ssh/id_rsa "$SERVER"
116
        else
117
        echo "Error no key found, please run 'ssh-keygen -t rsa' to generate one first."
118
        exit 4
119
        fi
120
        exit 0;;
121
    --)
122
        shift
123
        break;;
124
    esac
125
done
126
127
if [[ "$LIST" == "1" ]]; then
128
    servername
129
    ssh $SERVER "ls -la $DIRECTORY"
130
    exit
131
fi
132
  
133
FILENAME="$1"
134
135
# Using '20' as fixed numbers to ensure the 4 numbers are a date
136
if [[ "$FILENAME" =~ 20[0-9]{2} ]]; then
137
    echo "Filename ok!"
138
elif [[ -z "$FILENAME" ]]; then
139
    usage
140
    exit
141
else
142
    echo "Filename not valid! Did you include a date (Year-Month-Day)?"
143
    exit 1
144
fi
145
146
# Default upload directory is upload/ to ensure files are always atleast somewhat sorted
147
if [[ -z "$DIRECTORY" ]]; then
148
    DIRECTORY="upload/"
149
fi
150
151
file_on_server check_overwrite
152
153
154
# test if metadata file exists and add to files to upload
155
METAFILENAME="$FILENAME"
156
METAFILENAME=${METAFILENAME%.*}
157
METAFILENAME="$METAFILENAME.txt"
158
159
ALLFILE=$FILENAME
160
for i in $(ls); do
161
    if [[ "$i" == "$METAFILENAME" ]]; then
162
    ALLFILE="$ALLFILE $METAFILENAME"
163
    fi
164
done
165
166
# upload files with the same name
167
servername
168
rsync -vz --progress $ALLFILE "$SERVER":"$DIRECTORY"
169
170
#check if upload was successful
171
172
file_on_server
173
174
exit 0;
175
176