-
Notifications
You must be signed in to change notification settings - Fork 16
/
file.php
131 lines (118 loc) · 3.46 KB
/
file.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
require_once("lib/Thumb.php");
class File{
static function thumb($fname){
$height = isset($_GET["h"]) ? $_GET["h"] : 0;
$width = isset($_GET["w"]) ? $_GET["w"] : 0;
$picInfo = self::getThumbSize($width,$height);
if(file_exists( "thumbs/".$fname."_thumb")){
self::outputThumb("thumbs/".$fname."_thumb");
}
$thumbImg = new Thumb("uploads/".$fname);
$thumbImg->thumb($picInfo[1], $picInfo[0]);
if(!is_dir(dirname("thumbs/".$fname))){
mkdir(dirname("thumbs/".$fname),0777,true);
}
$thumbImg->out("thumbs/".$fname."_thumb");
self::outputThumb("thumbs/".$fname."_thumb");
}
static function outputThumb($path){
ob_end_clean();
header("Cache-Control: max-age=10800");
header('Content-Type: '.self::getMimetype($path));
$fileObj = fopen($path,"r");
echo fread($fileObj,filesize($path));
fclose($file);
}
static function update($fname,$content){
$filePath = 'uploads/' . $fname;
file_put_contents($filePath, "");
file_put_contents($filePath, $content);
}
static function getThumbSize($width,$height){
$rate = $width/$height;
$maxWidth = 90;
$maxHeight = 39;
$changeWidth = 39*$rate;
$changeHeight = 90/$rate;
if($changeWidth>=$maxWidth){
return [(int)$changeHeight,90];
}
return [39,(int)$changeWidth];
}
static function delete($fileList){
$fileList = json_decode(base64_decode($fileList),true);
foreach ($fileList as $key => $value) {
@unlink("uploads/".$value);
if(file_exists('thumbs/'.$value."_thumb")){
@unlink('thumbs/'.$value."_thumb");
}
}
}
static function preview($fname,$download = false){
$filePath = 'uploads/' . $fname;
@set_time_limit(0);
session_write_close();
$file_size = filesize($filePath);
$ranges = self::getRange($file_size);
if($ranges!=null){
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges:bytes');
header(sprintf('content-length:%u',$ranges['end']-$ranges['start']));
header(sprintf('content-range:bytes %s-%s/%s', $ranges['start'], $ranges['end']-1, $file_size));
}
if($download){
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($filePath));
header('Content-Disposition: filename='.str_replace(",","",isset($_GET["attaname"]) ? urldecode($_GET["attaname"]) : "download"));
ob_flush();
flush();
}
if(file_exists($filePath)){
if(!$download){
header('Content-Type: '.self::getMimetype($filePath));
ob_flush();
flush();
}
$fileObj = fopen($filePath,"rb");
fseek($fileObj, sprintf('%u', $ranges['start']));
while(!feof($fileObj)){
echo fread($fileObj,10240);
ob_flush();
flush();
}
fclose($fileObj);
}
}
static function getMimetype($path){
$finfoObj = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfoObj, $path);
finfo_close($finfoObj);
return $mimetype;
}
static function getRange($file_size){
if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])){
$range = $_SERVER['HTTP_RANGE'];
$range = preg_replace('/[\s|,].*/', '', $range);
$range = explode('-', substr($range, 6));
if(count($range)<2){
$range[1] = $file_size;
}
$range = array_combine(array('start','end'), $range);
if(empty($range['start'])){
$range['start'] = 0;
}
if(empty($range['end'])){
$range['end'] = $file_size;
}
return $range;
}
return null;
}
static function clean(){
@unlink("chunks");
@mkdir("chunks");
}
}
?>