]> git.plutz.net Git - rawnet/blob - page_channel.sh
more reliable video meta data check (duration)
[rawnet] / page_channel.sh
1 #!/bin/sh
2
3 chan_db="$_DATA/channels.db"
4
5 channel='' video='' action=''
6 path_info="$PATH_INFO"
7 path_info="${path_info#/channel/}"
8 if [ "$(checkid "${path_info%%/*}")" ]; then
9   channel="${path_info%%/*}"
10   path_info="${path_info#*/}"
11 fi
12 if [ "$(checkid "${path_info%%/*}")" ]; then
13   video="${path_info%%/*}"
14   path_info="${path_info#*/}"
15 fi
16 action="${path_info}"
17 unset path_info
18
19 # Channel
20 # ID    NAME    DESCRIPTION     LOGO    THEME   AUTHORS DESCR_CACHE FUTUREUSE
21
22 if [ "$channel" -a -f "$chan_db" -a -r "$chan_db" ]; then
23   read -r CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \
24           CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE \
25           CHANNEL_FUTUREUSE <<-EOF
26         $(grep "^${channel}     " "${chan_db}")
27         EOF
28   if [ "$CHANNEL_ID" ]; then
29            CHANNEL_NAME="$(UNSTRING "${CHANNEL_NAME}")"
30     CHANNEL_DESCRIPTION="$(UNSTRING "$CHANNEL_DESCRIPTION")"
31         CHANNEL_AUTHORS="$(UNSTRING "$CHANNEL_AUTHORS")"
32     CHANNEL_DESCR_CACHE="$(UNSTRING "$CHANNEL_DESCR_CACHE")"
33     vid_db="${_DATA}/${CHANNEL_ID}/videos.db"
34   else
35     channel=''
36   fi
37 fi
38
39 update_channel(){
40   local id="${1}" name description logo theme authors descr_cache futureuse
41   local ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE
42   local arg
43
44   for arg in "$@"; do case $arg in
45     name=*) name="${arg#*=}";;
46     description=*) description="${arg#*=}";;
47     logo=*) logo="${arg#*=}";;
48     theme=*) theme="${arg#*=}";;
49     authors=*) authors="${arg#*=}";;
50   esac; done
51
52   if LOCK "$chan_db"; then
53     while read -r ID NAME DESCRIPTION LOGO THEME AUTHORS DESCR_CACHE FUTUREUSE; do
54       if [ "$id" = "$ID" ]; then
55         printf '%s      %s      %s      %s      %s      %s      %s      %s\n' \
56                "$id" "$(STRING "${name-$(UNSTRING "$NAME")}")" \
57                "$(STRING "${description-$(UNSTRING "$DESCRIPTION")}")" \
58                "${logo:-${logo-${LOGO}}${logo+\\}}" \
59                "${theme:-${theme-${THEME}}${theme+\\}}" \
60                "$(STRING "${authors-$(UNSTRING "${AUTHORS}")}")" \
61                "$(printf %s "${description-$(UNSTRING "$DESCRIPTION")}" |markdown |STRING)" \
62                "${FUTUREUSE:-\\}"
63       else
64         printf '%s      %s      %s      %s      %s      %s      %s      %s\n' \
65                "$ID" "$NAME" "$DESCRIPTION" "$LOGO" "$THEME" "$AUTHORS" \
66                "$DESCR_CACHE" "$FUTUREUSE"
67       fi
68     done <"$chan_db" >"${chan_db}.$$"
69     mv -- "${chan_db}.$$" "${chan_db}"
70     RELEASE "$chan_db"
71   else
72     return 1
73   fi
74 }
75
76 AUTHOR(){
77   if [ "$CHANNEL_ID" -a "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ]; then
78     return 0
79   else
80     return 1
81   fi
82 }
83
84 # Video
85 # ID    NAME    DESCRIPTION     RESX    RESY    LENGTH  COVER   STATUS  UPLOADER        HITS
86
87 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
88   newchannel)
89     channel="$(POST channel |checkid)"
90     if [ ! "$USER_ID" ]; then
91       REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOTALLOWED"
92     elif LOCK "$chan_db"; then
93       if grep -q '^${channel}   ' "$chan_db"; then
94         RELEASE "$chan_db"
95         REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_EXISTS"
96       else
97         printf '%s      \\      \\      \\      \\      %s      \\      \\\n' \
98                "$channel" "$(STRING "$USER_ID")" \
99                >>"$chan_db"
100         RELEASE "$chan_db"
101         REDIRECT "${_BASE}/channel/${channel}/edit"
102       fi
103     else
104       REDIRECT "${_BASE}/channel/#ERROR_NEWCHANNEL_NOLOCK"
105     fi
106     ;;
107   update_channel)
108     if [ ! "$channel" ]; then
109       REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
110     elif [ ! "$USER_ID" ]; then
111       REDIRECT "${_BASE}/channel/${channel}/#ERROR_NOTLOGGEDIN"
112     elif ! AUTHOR; then
113       REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOTALLOWED"
114     elif update_channel "$channel" "name=$(POST name)" \
115                         "description=$(POST description)" \
116                         "authors=$USER_ID"; then
117       REDIRECT "${_BASE}/channel/${channel}/"
118     else
119       REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOLOCK"
120     fi
121     ;;
122   update_channel_cancel)
123     REDIRECT "${_BASE}/channel/${channel}/"
124     ;;
125   newvideo)
126     video="$(POST video |checkid)"
127
128     AUTHOR \
129     && mkdir -p -- "${_DATA}/${channel}/"
130
131     if [ ! "$video" ]; then
132       REDIRECT "${_BASE}/channel/${channel}/#ERROR_INVALID_ID"
133     elif [ ! "$channel" ]; then
134       REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
135     elif [ ! "$USER_ID" ]; then
136       REDIRECT "${_BASE}/channel/${channel}/#ERROR_NOTLOGGEDIN"
137     elif ! AUTHOR; then
138       REDIRECT "${_BASE}/channel/${channel}/#ERROR_UPDATE_NOTALLOWED"
139     elif LOCK "$vid_db"; then
140       if grep -q '^${video}     ' "$vid_db"; then
141         RELEASE "$vid_db"
142         REDIRECT "${_BASE}/channel/${channel}/#ERROR_NEWVIDEO_EXISTS"
143       else
144                 # ID    NAME    DESC    RESX    RESY    LENGTH  COVER   STATUS  UPLOADER HITS   FUTUREUSE
145         printf '%s      \\      \\      \\      \\      \\      \\      private %s      \\      \\\n' \
146                "$video" "$(STRING "$USER_ID")" \
147                >>"$vid_db"
148         RELEASE "$vid_db"
149         REDIRECT "${_BASE}/channel/${channel}/${video}/edit"
150       fi
151     else
152       REDIRECT "${_BASE}/channel/${channel}/#ERROR_NEWVIDEO_NOLOCK"
153     fi
154     ;;
155 esac
156
157 w_video(){
158   local thumb
159   local VIDEO_ID VIDEO_NAME VIDEO_DESCRIPTION VIDEO_RESX VIDEO_RESY \
160         VIDEO_LENGTH VIDEO_COVER VIDEO_STATUS VIDEO_UPLOADER VIDEO_HITS \
161         VIDEO_DESCR_CACHE VIDEO_FUTUREUSE
162
163   if read -r VIDEO_ID VIDEO_NAME VIDEO_DESCRIPTION VIDEO_RESX VIDEO_RESY \
164              VIDEO_LENGTH VIDEO_COVER VIDEO_STATUS VIDEO_UPLOADER VIDEO_HITS \
165              VIDEO_DESCR_CACHE VIDEO_FUTUREUSE; then
166            VIDEO_NAME="$(UNSTRING "$VIDEO_NAME")"
167     VIDEO_DESCRIPTION="$(UNSTRING "$VIDEO_DESCRIPTION")"
168     VIDEO_DESCR_CACHE="$(UNSTRING "$VIDEO_DESCR_CACHE")"
169
170     [ "${VIDEO_STATUS}" = public ] || AUTHOR || return 0
171
172     thumb="${_BASE}/video/${CHANNEL_ID}/${VIDEO_ID}_thumb.jpg"
173     [ "$NAME" = \\ ] && NAME="(Unnamed Video)"
174     printf '[div .video .thumb
175               [h3 [a href="%s/channel/%s/%s/" . %s]]
176               [figure [img src="%s" alt=""]]
177               [div .description . %s]
178             ]' "$_BASE" "$CHANNEL_ID" "$VIDEO_ID" \
179                "$(HTML "${VIDEO_NAME:-(Unnamed Video)}")" \
180                "$thumb" "$(UNSTRING "$DESCR_CACHE")"
181   else
182     return 1
183   fi
184 }
185
186 w_channel(){
187   local vid_db
188   local CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \
189         CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE CHANNEL_FUTUREUSE
190
191   if read -r CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \
192              CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE \
193              CHANNEL_FUTUREUSE; then
194            CHANNEL_NAME="$(UNSTRING "$CHANNEL_NAME")"
195     CHANNEL_DESCRIPTION="$(UNSTRING "$CHANNEL_DESCRIPTION")"
196         CHANNEL_AUTHORS="$(UNSTRING "$CHANNEL_AUTHORS")"
197     CHANNEL_DESCR_CACHE="$(UNSTRING "$CHANNEL_DESCR_CACHE")"
198
199     vid_db="${_DATA}/${CHANNEL_ID}/videos.db"
200     cat <<-EOF
201         [div .channel
202           [div .description
203             [h2 [a href="${_BASE}/channel/${CHANNEL_ID}/" $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]]
204             ${CHANNEL_DESCR_CACHE}
205           ]$(
206             [ -f "$vid_db" -a -r "$vid_db" ] \
207             && while w_video; do :; done <"$vid_db"
208           )
209         ]
210         EOF
211   else
212     return 1
213   fi
214 }
215
216 w_channel_list(){
217   if [ $USER_ID ]; then
218     printf '
219     [form .channel .newchannel method=POST
220       [hidden "channel" "%s"]
221       [submit "action" "newchannel" New Channel]
222     ]' "$(timeid)"
223   fi
224   [ -f "$chan_db" -a -r "$chan_db" ] \
225   && while w_channel; do :; done <"$chan_db"
226 }
227
228 if [ "$channel" -a "$video" ]; then
229   . ${_EXEC}/page_video.sh
230 elif [ "$channel" -a "$action" = edit ]; then
231   AUTHOR || REDIRECT "${_BASE}/${channel}/#ERROR_EDIT_NOTALLOWED"
232   yield_page "$CHANNEL_NAME - Edit" "channel edit" <<-EOF
233         [form .channel .edit method=POST
234           [input name="name" value="$(HTML "$CHANNEL_NAME")" placeholder="Channel Name"]
235           [textarea name="description" placeholder="Description" . $(HTML "$CHANNEL_DESCRIPTION")]
236           [submit "action" "update_channel" . Update]
237           [submit "action" "update_channel_cancel" . Cancel]
238         ]
239         EOF
240 elif [ "$channel" ]; then
241   yield_page "$CHANNEL_NAME" "channel" <<-EOF
242         [nav [a href="../" Channels] - [span $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]
243           $(AUTHOR && printf ' - [a href="edit" edit]')
244         ]
245         [h1 .name $(HTML "$CHANNEL_NAME")]
246         [div .description . ${CHANNEL_DESCR_CACHE}]
247         [h1 .videos Videos]
248         [div .videos . $(
249           AUTHOR && printf '
250              [form .video .newvideo method=POST
251                [hidden "video" "%s"]
252                [submit "action" "newvideo" New Video]
253              ]' "$(timeid)"
254           [ -f "$vid_db" -a -r "$vid_db" ] \
255           && while w_video "$ID"; do :; done <"$vid_db"
256         )]
257         EOF
258 else
259   yield_page "Channels" "channels" <<-EOF
260         $(w_channel_list)
261         EOF
262 fi