]> git.plutz.net Git - rawnet/blob - page_video.sh
filter non-public videos, smarter update_channel function
[rawnet] / page_video.sh
1 #!/bin/sh
2
3 if [ "$video" -a -f "$vid_db" -a -r "$vid_db" ]; then
4   read -r VIDEO_ID VIDEO_NAME VIDEO_DESCRIPTION VIDEO_RESX VIDEO_RESY \
5           VIDEO_LENGTH VIDEO_COVER VIDEO_STATUS VIDEO_UPLOADER VIDEO_HITS \
6           VIDEO_DESCR_CACHE VIDEO_FUTUREUSE <<-EOF
7         $(grep "^${video}       " "${vid_db}")
8         EOF
9   if [ "$VIDEO_ID" ]; then
10            VIDEO_NAME="$(UNSTRING "$VIDEO_NAME")"
11     VIDEO_DESCRIPTION="$(UNSTRING "$VIDEO_DESCRIPTION")"
12     VIDEO_DESCR_CACHE="$(UNSTRING "$VIDEO_DESCR_CACHE")"
13   else
14     video=''
15   fi
16 fi
17
18 # Video
19 # ID    NAME    DESCRIPTION     RESX    RESY    LENGTH  COVER   STATUS (void|private|hidden|public)     UPLOADER        HITS    DESCR_CACHE     FUTUREUSE
20
21 update_video(){
22   local id="${1}" name="${2}" description="${3}" resx="${4}" resy="${5}" \
23         length="${6}" cover="${7}" status="${8}" uploader="${9}" \
24         hits="${10}" descr_cache="${11}" futureuse="${12}"
25   local ID INFO
26
27   if LOCK "$vid_db"; then
28     while read -r ID INFO; do
29       if [ "$id" = "$ID" ]; then
30                 # ID    NAME    DESCRIPTION RESX RESY   LENGTH  COVER   STATUS  UPLOADER HITS   DESCR_CACHE     FUTUREUSE
31         printf '%s      %s      %s      %i      %i      %i      %s      %s      %s      %i      %s      %s\n' \
32                "$id" "$(STRING "$name")" "$(STRING "$description")" "$resx" "$resy" "$length" \
33                "$(STRING "$cover")" "${status:-void}" "${uploader:-\\}" "$hits" \
34                "$(printf %s "$description" |markdown |STRING)" "${futureuse:-\\}"
35       else
36         printf '%s      %s\n' "$ID" "$INFO"
37       fi
38     done <"$vid_db" >"${vid_db}.$$"
39     mv -- "${vid_db}.$$" "${vid_db}"
40     RELEASE "$vid_db"
41   else
42     return 1
43   fi
44 }
45
46 UPLOAD(){
47   local file="$1"
48   local boundary line last
49   local CONTENT_TYPE="$(HEADER Content-Type)"
50
51   [ ! "${CONTENT_TYPE}" -o "${CONTENT_TYPE##multipart/form-data;*}" ] && return 1
52
53   boundary="${CONTENT_TYPE#*; boundary=}"
54   boundary="${boundary%%;*}"
55
56   head -c "$CONTENT_LENGTH" \
57   | sed -nE '
58     /^--'"${boundary}"'\r?$/!b;
59     :A; n;
60     /^\r?$/!bA;
61      n;
62     :FILE
63     p; n;
64     /^--'"${boundary}"'(--)?\r?$/!bFILE;
65     :END
66     $q; n; bEND;
67   ' >"$file"
68   truncate -s $(( $(stat -c %s -- "$file") -2 )) -- "$file"
69 }
70
71 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
72   update_video)
73     if [ ! "$USER_ID" ]; then
74       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOTLOGGEDIN"
75     elif ! AUTHOR; then
76       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOTALLOWED"
77     elif update_video "$video" "$(POST name)" "$(POST description)" 0 0 0 \
78                       "" "void" "$USER_ID" 0 ""; then
79       REDIRECT "${_BASE}/channel/${channel}/${video}/#UPDATE_SUCCESS"
80     else
81       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOLOCK"
82     fi
83     ;;
84   update_video_cancel)
85     REDIRECT "${_BASE}/channel/${channel}/${video}/#CANCELED"
86     ;;
87 esac
88
89 if [ "$REQUEST_METHOD" = POST -a "$channel" -a "$video" ]; then
90   if ! AUTHOR; then
91     head -c "$CONTENT_LENGTH" >/dev/null
92     REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPLOAD_NOTALLOWED"
93   elif [ "$VIDEO_STATUS" != void ]; then
94     head -c "$CONTENT_LENGTH" >/dev/null
95     REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPLOAD_NOCLOBBER"
96   elif UPLOAD "$_DATA/$channel/$video.mp4"; then
97     update_video "$video" "$VIDEO_NAME" "$VIDEO_DESCRIPTION" 0 0 0 \
98                  "$VIDEO_COVER" private "$VIDEO_UPOADER" "$VIDEO_HITS" \
99                  "" ""
100   fi
101 fi
102
103 if [ "$channel" -a "$video" -a "$action" = edit ]; then
104   yield_page "$VIDEO_NAME - Edit" "video edit" <<-EOF
105         [form .video .edit method=POST
106           [input name="name" value="$(HTML "$VIDEO_NAME")" placeholder="Video Name"]
107           [fieldset .status $([ $VIDEO_STATUS = void ] && printf "disabled=disabled")
108             [radio "status" "void" #status_private $(checked $VIDEO_STATUS private void)]
109               [label for=status_private tooltip="Video is only visible to channel authors" Private]
110             [radio "status" "void" #status_hidden  $(checked $VIDEO_STATUS hidden)]
111               [label for=status_hidden tooltip="Video will not be listed but can be viewed by anyone knowing the URL" Hidden]
112             [radio "status" "void" #status_public $(checked $VIDEO_STATUS public)]
113               [label for=status_public tooltip="Video will be listed publicly" Public]
114           ]
115           [textarea name="description" placeholder="Description" . $(HTML "$VIDEO_DESCRIPTION")]
116           [submit "action" "update_video" . Update]
117           [submit "action" "update_video_cancel" . Cancel]
118         ]
119         EOF
120 elif [ "$channel" -a "$video" ]; then
121   yield_page "$VIDEO_NAME" "video edit" <<-EOF
122         [nav [a href="../../" Channels] - [a href="../" $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")] - [span $(HTML "${VIDEO_NAME:-(Unnamed Video)}")]
123           $(AUTHOR && printf ' - [a href="edit" edit]')
124         ]
125         $( AUTHOR && [ $VIDEO_STATUS = void ] && printf '
126         [form .upload method=POST enctype="multipart/form-data"
127           [input type=file name=upload]
128           [submit "action" "video_upload" Upload]
129         ]')
130         $( [ $VIDEO_STATUS != void ] && printf '
131         [video
132           [source src="%s/video/%s/%s.mp4"]
133         ]' "$_BASE" "$channel" "$video"
134         )
135         [h1 .name $(HTML "$VIDEO_NAME")]
136         [div .description . ${VIDEO_DESCR_CACHE}]
137         EOF
138 else
139   . "$_EXEC/page_404.sh"
140 fi