Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 02002b4

Browse files
committed
Roll back to non-cURL based download.php
1 parent b499b59 commit 02002b4

File tree

4 files changed

+22
-54
lines changed

4 files changed

+22
-54
lines changed

Dockerfile

-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@ COPY --chown=1000:1000 ["composer.json", "robots.txt", "Download.php", "./"]
1717
RUN composer install --no-dev --no-plugins --no-scripts --no-cache -n -o -v
1818
RUN rm composer.json
1919

20-
USER ROOT
21-
WORKDIR /download
22-
COPY --chown=1000:1000 ["robots.txt", "Download.php", "./"]
23-
2420
FROM webdevops/php-nginx:7.4-alpine AS Run
2521
WORKDIR /api
2622
COPY --from=Build --chown=1000:1000 /api /api
27-
COPY --from=Build --chown=1000:1000 /download /download
2823

2924
ENV FPM_PM_START_SERVERS 2
3025
ENV FPM_PM_MIN_SPARE_SERVERS 1

Download.php

+20-47
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,37 @@
11
<?php
22

3-
function download($file_source, $file_target) {
4-
$rh = fopen($file_source, 'rb');
5-
$wh = fopen($file_target, 'w+b');
6-
if (!$rh || !$wh) {
7-
return false;
8-
}
3+
use com\carlgo11\tempfiles\datastorage\DataStorage;
94

10-
while (!feof($rh)) {
11-
if (fwrite($wh, fread($rh, 4096)) === FALSE) {
12-
return false;
13-
}
14-
echo '';
15-
flush();
16-
}
17-
18-
fclose($rh);
19-
fclose($wh);
20-
21-
return true;
22-
}
5+
require __DIR__ . '/src/com/carlgo11/tempfiles/autoload.php';
236

247
function return404() {
258
$notFoundURL = filter_input(INPUT_ENV, 'TMP_404_URL', FILTER_VALIDATE_URL, ['options' => ['default' => 'https://tempfiles.download/download/?404=1']]);
269
header($_SERVER['SERVER_PROTOCOL'] . " 404 File Not Found");
27-
header("Location: $notFoundURL");
28-
exit;
10+
//header("Location: $notFoundURL");
11+
//exit;
2912
}
3013

3114
$url = explode('/', strtoupper($_SERVER['REQUEST_URI']));
3215
$id = filter_var($url[1]);
3316
$password = filter_input(INPUT_GET, "p");
3417

35-
# API Download URL
36-
$downloadURL = filter_input(INPUT_ENV, 'TMP_API_DOWNLOAD_URL', FILTER_VALIDATE_URL, ['options' => ['default' => 'https://api.tempfiles.download/download/?id=%1$s&p=%2$s']]);
37-
$d_url = sprintf($downloadURL, $id, $password);
38-
39-
$result = download($d_url, "/tmp/$id.tmp");
40-
if (!$result)
41-
return404();
42-
43-
// Execute cURL command and get response data
44-
$response = json_decode(curl_exec(file_get_contents("/tmp/$id.tmp")));
18+
$file = DataStorage::getFile($id, $password);
4519

46-
unlink("/tmp/$id.tmp");
20+
$metadata = $file->getMetaData();
21+
$content = base64_encode($file->getContent());
4722

23+
if ($file->getMaxViews()) { // max views > 0
24+
if ($file->getMaxViews() <= $file->getCurrentViews() + 1) DataStorage::deleteFile($id);
25+
else $file->setCurrentViews($file->getCurrentViews() + 1);
26+
}
4827

28+
// Set headers
29+
header("Content-Description: File Transfer");
30+
header("Expires: 0");
31+
header("Pragma: public");
32+
header("Content-Type: {$metadata['type']}");
33+
header("Content-Disposition: inline; filename=\"{$metadata['name']}\"");
34+
header("Content-Length: {$metadata['size']}");
4935

50-
if ($response->data) {
51-
52-
// Set headers
53-
header("Content-Description: File Transfer");
54-
header("Expires: 0");
55-
header("Pragma: public");
56-
header("Content-Type: {$response->type}");
57-
header("Content-Disposition: inline; filename=\"{$response->filename}\"");
58-
header("Content-Length: {$response->length}");
59-
60-
// output file contents
61-
echo base64_decode($response->data);
62-
63-
} else return404();
64-
exit;
36+
// output file contents
37+
echo base64_decode($content);

resources/nginx.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ server {
1212
server {
1313
listen 5393;
1414
server_name _;
15-
root "/download";
15+
root "/api/";
1616
index Download.php;
1717
rewrite ^/(.*)+$ /Download.php?$1;
1818

src/com/carlgo11/tempfiles/datastorage/FileStorage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private function getExpiry(string $id) {
8989

9090
$file = file_get_contents($conf['file-path'] . $id);
9191
$data = json_decode($file, TRUE);
92-
if ($data === NULL) return NULL;
92+
if ($data === NULL || $data === FALSE) return NULL;
9393
return $data['expiry'];
9494
}
9595

0 commit comments

Comments
 (0)