Skip to content

Commit 80a70a0

Browse files
author
Niko
committed
add expiry option, add cron container with automatic deletion of expired content
1 parent 39a5c6c commit 80a70a0

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

build/index.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
if(!isset($_FILES['file'])) {
3-
?>
3+
?>
44
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8" /><title>file sharing</title>
55
<style type="text/css">
66
a {
@@ -24,6 +24,30 @@
2424
$newdir = substr(md5(uniqid()), 0, 12);
2525
exec('mkdir '.$basedir.'/'.$newdir);
2626
exec('mv '.$_FILES['file']['tmp_name'].' '.$basedir.'/'.$newdir.'/'.$newFName);
27+
if (isset($_REQUEST['exp'])) {
28+
preg_match('/^(\d+)([hdwmy]?)$/', $_REQUEST['exp'], $matches);
29+
if ($matches[1] && $matches[2]) {
30+
switch ($matches[2]) {
31+
case 'h':
32+
$hours = $matches[1];
33+
break;
34+
case 'd':
35+
$hours = 24 * $matches[1];
36+
break;
37+
case 'w':
38+
$hours = 24 * 7 * $matches[1];
39+
break;
40+
case 'm':
41+
$hours = 24 * 30.5 * $matches[1];
42+
break;
43+
case 'y':
44+
$hours = 24 * 365 * $matches[1];
45+
break;
46+
}
47+
$expiry = mktime() + ($hours * 60 * 60);
48+
exec('touch '.$basedir.'/'.$newdir.'/expires-at-'.$expiry);
49+
}
50+
}
2751
if (stristr($_SERVER['HTTP_USER_AGENT'], 'curl')) {
2852
echo "https://".$_SERVER['HTTP_HOST']."/dl/".$newdir."/".$newFName;
2953
echo "\n";

cron/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM alpine:3
2+
3+
RUN apk add bash dcron
4+
RUN mkdir -p /var/logs
5+
6+
# 20 minute interval
7+
ENV EXECUTION_CRON_EXPRESSION */20 * * * *
8+
9+
COPY entrypoint.sh /
10+
COPY run-cron.sh /
11+
RUN chmod +x /entrypoint.sh
12+
RUN chmod +x /run-cron.sh
13+
ENTRYPOINT ["/entrypoint.sh"]

cron/entrypoint.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
trap 'exit 0' SIGTERM | tee -a /var/logs/cron.log
4+
5+
cat > crontab.tmp << EOF
6+
$EXECUTION_CRON_EXPRESSION /run-cron.sh $@ | tee -a /var/logs/cron.log
7+
# An empty line is required at the end of this file for a valid cron file.
8+
EOF
9+
10+
crontab "crontab.tmp"
11+
rm -f "crontab.tmp"
12+
/usr/sbin/crond -L /var/logs/cron.log
13+
tail -n 5000 -f /var/logs/cron.log

cron/run-cron.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash -eux
2+
3+
DL_DIR=/var/www/dl
4+
5+
if [ ! -d ${DL_DIR} ]; then
6+
exit 0
7+
fi
8+
9+
CURRENT_TS=$(date '+%s')
10+
find "${DL_DIR}" -type f -name "expires-at-*" | while read expires_file; do
11+
# /var/www/dl/c1984d98fcc6/expires-at-1585729013
12+
TS=$(basename ${expires_file} | sed -E 's/^expires-at-(\d+)$/\1/g')
13+
if [ $(echo ${TS} | grep -q '^\d\+$') -gt 0 ]; then
14+
continue;
15+
fi
16+
if (( CURRENT_TS > TS )); then
17+
echo -en $(date -d @${CURRENT_TS})
18+
echo -en " > "
19+
echo -en $(date -d @${TS})
20+
echo " >> DELETING $(dirname ${expires_file})"
21+
rm -r $(dirname ${expires_file})
22+
fi
23+
done

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ services:
1111
- file_storage:/var/www/dl
1212
ports:
1313
- 127.0.0.1:8023:80
14+
cron:
15+
build: cron/
16+
restart: unless-stopped
17+
volumes_from:
18+
- server

0 commit comments

Comments
 (0)