X-Git-Url: http://git.plutz.net/?p=serve0;a=blobdiff_plain;f=index.cgi;h=ad110e070dcfeebb301108fc4bf16d53a72c3324;hp=ec7572914a9d8f3bfbb6b1cf8d1925b1099936c9;hb=16ee00076365ddf4a1b85396c6673f2adbf215e8;hpb=f6d9912760f1c53189c0928ef58a7ff6a158a768 diff --git a/index.cgi b/index.cgi index ec75729..ad110e0 100755 --- a/index.cgi +++ b/index.cgi @@ -1,70 +1,106 @@ -#!/bin/zsh +#!/bin/sh -# Copyright 2014 Paul Hänsch -# -# This file is part of Serve0. -# -# Serve0 is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Serve0 is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Serve0. If not, see . +for n in "$@"; do case ${n%%=*} in + data) _DATA="${n#data=}";; + exec) _exec="${n#exec=}";; + noerr) exec 2>&-;; +esac; done -export LC_ALL=C +[ -z "${_EXEC%/}" ] && _EXEC="$(realpath "${0%/*}")" || _EXEC="${_EXEC%/}" +[ -z "${_DATA%/}" ] && _DATA=. || _DATA="${_DATA%/}" -# basic functions -die() { - echo "$*" >/dev/stderr - exit 1 -} -debug() { #change to false to disable debugging - #true && echo "$*" >>debug - true && [ -n "$*" ] && echo -E "$*" >>/dev/stderr - true && [ -z "$*" ] && tee /dev/stderr -} +file_pattern='^.*\.(mov|ts|mpg|mpeg|mp4|m4v|avi|mkv|flv|sfv|wmv|ogm|ogv|webm|iso|rmvb)$' -# this program is supposed to be symlinked into a http root directory -# we will use the http root as object storage (data directory) and call sub -# programs from the directory in which the real executable resides -# therefore we need to identify the code and data directories _EXEC and _DATA -call="$0" -real="$(readlink -f $call)" -_EXEC="$(dirname "$real")" #execution directory -_DATA="$(dirname "$call")" #storage directory +. "$_EXEC/cgilite/cgilite.sh" -[ -w "$_DATA" ] && [ -d "$_DATA" ] || die "storage directory must be writable" +FILTER="$(GET f)" +SEARCH="$(GET s)" +ORDER="$(GET o |grep -m1 -axE 'Date|Name|Length' || printf Name)" +LISTSIZE="$(COOKIE pagesize |grep -m1 -axE '[1-9][0-9]*' || printf 50)" +ITEM="$(PATH "${PATH_INFO#/}")" +ACTION="$(GET a)" -# create directories for object storage -for each in "$_DATA"/{cache,meta,mp4,videos,thumbs,trash,by_length}; do - [ ! -e "$each" ] && mkdir "$each" - [ -w "$each" ] && [ -d "$each" ] || die "storage $each must be a writable directory" -done +case $ACTION in + setprefs) + SET_COOKIE +$((86400 * 90)) pagesize="$(POST pagesize |grep -m1 -axE '[1-9][0-9]*' || printf 50)" + SET_COOKIE +$((86400 * 90)) mode="$(POST mode |grep -m1 -axE 'browse|index' || printf browse)" + SET_COOKIE +$((86400 * 90)) fakemp4="$(POST fakemp4 |grep -m1 -axE 'yes' || printf no)" + SET_COOKIE +$((86400 * 90)) downscale="$(POST downscale |grep -m1 -axE 'yes' || printf no)" + [ "$(POST index)" = "update" ] && touch -cd @0 "${_DATA}/.index/meta" + REDIRECT "$(POST ref)" + ;; + bookmark) + bm="$_DATA/.index/bookmarks" + . "$_EXEC/cgilite/storage.sh" -# create htaccess file -[ -f .htaccess ] || cat >.htaccess <"$bm.tmp" + [ ! "$(POST delete)" ] \ + && printf '%s search=%s filter=%s\r\n' \ + "$(POST name |STRING)" "$s" "$f" >>"$bm.tmp" + mv "$bm.tmp" "$bm" + RELEASE "$bm" + fi + REDIRECT "$(POST ref)" + ;; + multitag) + . "$_EXEC/multitag.sh" + REDIRECT "$(POST ref)" + ;; +esac -. "$_EXEC/cgi.sh" - -cgi_get - -. "$_EXEC/constants.sh" - -if [ -n "$_GET[\"action\"]" ]; then - . "$_EXEC/action.sh" -elif [ -n "$_GET[\"export\"]" ]; then - . "$_EXEC/export.sh" +if [ "$ITEM" = "/style.css" ]; then + . "$_EXEC/cgilite/file.sh" + [ -r "$_DATA/$ITEM" ] && FILE "$_DATA/$ITEM" \ + || FILE "$_EXEC/style.css" +elif [ "$ITEM" = "/stereoview.js" ]; then + . "$_EXEC/cgilite/file.sh" + FILE "$_EXEC/stereoview.js" +elif [ -f "$_DATA/$ITEM" ]; then + case $ACTION in + thumbnail) + . "$_EXEC/cgilite/file.sh" + . "$_EXEC/thumbnail.sh" + index="$_DATA/${ITEM%/*}/.index" + thumb="$index/${ITEM##*/}"; thumb="${thumb%.*}.jpg" + [ -d "$index" -a ! -f "$thumb" ] \ + && { printf %s "$ITEM" |grep -qE -e "${file_pattern}" ;} \ + && gen_thumb "$_DATA/$ITEM" "$thumb" + FILE "$thumb" + ;; + delete) + ;; + download) + . "$_EXEC/cgilite/file.sh" + fakemp4="$(COOKIE fakemp4)" + downscale="$(COOKIE downscale)" + downfile="$_DATA/${ITEM%/*}/.transcode/${ITEM%.*}.480p.webm" + if [ "$downscale" = yes -a -f "$downfile" ]; then + FILE "$downfile" "$([ "$fakemp4" = yes ] && printf 'video/mp4')" + else + FILE "$_DATA/$ITEM" "$([ "$fakemp4" = yes ] && printf 'video/mp4')" + fi + ;; + *) . "$_EXEC/view.sh" + esac +elif [ -d "$_DATA/$ITEM" ]; then + case $ACTION in + advsearch) + . "$_EXEC/advsearch.sh" + ;; + spawnindex) + if [ "$(POST recursive)" = yes ]; then + find "$_DATA/$ITEM" -depth -type d \! -name .index \ + -exec mkdir -p '{}'/.index \; + else + mkdir -p "$_DATA/$ITEM/.index" + fi + REDIRECT "$(POST ref)" + ;; + *) . "$_EXEC/list.sh" + ;; + esac else - . "$_EXEC/page.sh" + printf 'Status: 404 Not Found\r\nContent-Length 0:\r\n\r\n' fi