-
Notifications
You must be signed in to change notification settings - Fork 0
merge changes from master #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 5.0
Are you sure you want to change the base?
Changes from 22 commits
7e6cd67
c3b25c1
517a0f8
85201af
d9cad6d
ca96849
a68aa7f
92c16fe
ee20fb2
99ece5b
7763d9d
e04b68d
c863662
66d4b64
b0a6d87
187a217
6026168
fbcdbae
e25b3dd
564f79f
cf668f0
b8cad00
547adaa
140ad18
f2803cb
d29a25b
a38ba5b
12db687
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
| use Migrations\AbstractMigration; | ||
|
|
||
| class AddSequenceIndexToAttachments extends AbstractMigration | ||
| { | ||
| /** | ||
| * Change Method. | ||
| * | ||
| * More information on this method is available here: | ||
| * http://docs.phinx.org/en/latest/migrations.html#the-change-method | ||
| * @return void | ||
| */ | ||
| public function change() | ||
| { | ||
| $table = $this->table('attachments'); | ||
| $table->addIndex(['model', 'foreign_key', 'sequence'], ['name' => 'model_foreign_key_sequence']); | ||
| $table->update(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,6 +115,37 @@ public function image($id) | |
| $options = []; | ||
| foreach ($validOptions as $option) { | ||
| if ($this->request->getQuery($option)) { | ||
| //validate quality | ||
| if ($option == 'q' && ( | ||
| !is_numeric($this->request->getQuery($option)) || | ||
| $this->request->getQuery($option) > 100) || | ||
| $this->request->getQuery($option) < 0 | ||
| ) { | ||
| throw new \Exception("Invalid quality parameter."); | ||
| } | ||
| //validate height and width | ||
| if (($option == 'w' || $option == 'h') && ( | ||
| !is_numeric($this->request->getQuery($option)) || | ||
| $this->request->getQuery($option) < 0 | ||
| )) { | ||
| throw new \Exception("Invalid height/width parameter."); | ||
| } | ||
| //validate crop and enlarge | ||
| if (($option == 'c' || $option == 'e') && ( | ||
| $this->request->getQuery($option) != 0 && | ||
| $this->request->getQuery($option) != 1 | ||
| )) { | ||
| throw new \Exception("Invalid crop/enlarge parameter."); | ||
| } | ||
| //validate mode | ||
| if ($option == 'm' && $this->request->getQuery($option) != 'fill') { | ||
| throw new \Exception("Invalid mode parameter."); | ||
| } | ||
| //validate fill color | ||
| if ($option == 'fc' && !preg_match('/^[a-f0-9]{6}$/i', $this->request->getQuery($option))) { | ||
| throw new \Exception("Invalid fill color parameter."); | ||
| } | ||
|
|
||
| $options[$option] = $this->request->getQuery($option); | ||
| } //default fill color to white | ||
| elseif ($option == 'fc') { | ||
|
|
@@ -140,7 +171,6 @@ function ($v, $k) { | |
| array_keys($options) | ||
| )); | ||
| $cacheFile = $cacheFolder . DS . md5($id . $cacheKey); | ||
|
|
||
| if (!file_exists($cacheFile)) { | ||
| if (!file_exists($cacheFolder)) { | ||
| mkdir($cacheFolder); | ||
|
|
@@ -155,7 +185,11 @@ function ($v, $k) { | |
| if ($attachment->filetype === 'application/pdf') { | ||
| $imagePath = "/tmp/" . uniqid(); | ||
| $imagick = new \Imagick("{$attachment->path}[0]"); | ||
| $imagick->setResolution(300, 300); | ||
| $imagick->setBackgroundColor('white'); | ||
| $imagick->setImageFormat('jpg'); | ||
| $imagick->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN); | ||
| $imagick->setImageAlphaChannel(\Imagick::ALPHACHANNEL_REMOVE); | ||
| file_put_contents($imagePath, $imagick); | ||
| } | ||
| $image = new ImageResize($imagePath); | ||
|
|
@@ -166,7 +200,7 @@ function ($v, $k) { | |
| $image->save($tempImage, IMAGETYPE_JPEG); | ||
|
|
||
| $image = new ImageResize($imagePath); | ||
| $image->resize($options['w'], $options['h']); | ||
| $image->resize($options['w'], $options['h'], true); | ||
| $image->addFilter(function ($imageDesc) use ($options, $tempImage) { | ||
| list($r, $g, $b) = sscanf($options['fc'], "%02x%02x%02x"); | ||
| $backgroundColor = imagecolorallocate($imageDesc, $r, $g, $b); | ||
|
|
@@ -202,6 +236,10 @@ function ($v, $k) { | |
| //preserve PNG for transparency | ||
| if ($attachment->filetype == 'image/png' && $options['type'] != IMAGETYPE_WEBP) { | ||
| $options['type'] = IMAGETYPE_PNG; | ||
| //modify quality imagejpeg to imagepng | ||
| if (!is_null($options['q'])) { | ||
| $options['q'] = (int) round((100 - $options['q']) / 10); | ||
| } | ||
| } | ||
| $image->save($cacheFile, $options['type'], $options['q']); | ||
| } | ||
|
|
@@ -211,10 +249,17 @@ function ($v, $k) { | |
| $file = new File($cacheFile); | ||
| $response = $this->response->withFile($cacheFile, | ||
| ['download' => false, 'name' => (isset($attachment) ? $attachment->filename : null)]) | ||
| ->withVary('Accept') | ||
| ->withType($file->mime()) | ||
| ->withCache('-1 minute', '+1 month') | ||
| ->withExpires('+1 month') | ||
| ->withCache('-1 minute', '+6 month') | ||
| ->withExpires('+6 month') | ||
| ->withMustRevalidate(false) | ||
| ->withModified($file->lastChange()); | ||
|
|
||
| if ($options['type'] == IMAGETYPE_WEBP) { | ||
| $response = $response->withSharable(false); | ||
| } | ||
|
|
||
| if ($response->checkNotModified($this->request)) { | ||
| return $response; | ||
| } | ||
|
|
@@ -228,8 +273,21 @@ public function file($id, $name = null) | |
| if (!file_exists($attachment->path)) { | ||
| throw new \Exception("File {$attachment->path} cannot be read."); | ||
| } | ||
| $response = $this->response->withType($attachment->filetype) | ||
| ->withFile($attachment->path, ['download' => false, 'name' => $attachment->filename]); | ||
| $file = new File($attachment->filetype); | ||
|
||
|
|
||
| $response = $this->response->withFile($attachment->path, | ||
| ['download' => false, 'name' => $attachment->filename]) | ||
| ->withType($attachment->filetype) | ||
| ->withCache('-1 minute', '+6 month') | ||
| ->withExpires('+6 month') | ||
| ->withMustRevalidate(false) | ||
| ->withModified($file->lastChange()) | ||
| ->withSharable(true); | ||
|
|
||
| if ($response->checkNotModified($this->request)) { | ||
| return $response; | ||
| } | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
|
|
@@ -239,8 +297,20 @@ public function download($id, $name = null) | |
| if (!file_exists($attachment->path)) { | ||
| throw new \Exception("File {$attachment->path} cannot be read."); | ||
| } | ||
| $response = $this->response->withType($attachment->filetype) | ||
| ->withFile($attachment->path, ['download' => true, 'name' => $attachment->filename]); | ||
| $file = new File($attachment->filetype); | ||
|
||
|
|
||
| $response = $this->response->withFile($attachment->path, | ||
| ['download' => true, 'name' => $attachment->filename]) | ||
| ->withType($attachment->filetype) | ||
| ->withCache('-1 minute', '+6 month') | ||
| ->withExpires('+6 month') | ||
| ->withMustRevalidate(false) | ||
| ->withModified($file->lastChange()); | ||
|
|
||
| if ($response->checkNotModified($this->request)) { | ||
| return $response; | ||
| } | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
|
|
@@ -314,13 +384,13 @@ public function stream($id, $name = null) | |
| ob_get_clean(); | ||
| header("Content-Type: video/mp4"); | ||
| header("Cache-Control: max-age=311040000, public"); | ||
| header("Expires: ".gmdate('D, d M Y H:i:s', time()+311040000) . ' GMT'); | ||
| header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($attachment->path)) . ' GMT' ); | ||
| header("Expires: " . gmdate('D, d M Y H:i:s', time() + 311040000) . ' GMT'); | ||
| header("Last-Modified: " . gmdate('D, d M Y H:i:s', @filemtime($attachment->path)) . ' GMT'); | ||
| $this->start = 0; | ||
| $this->size = filesize($attachment->path); | ||
| $this->end = $this->size - 1; | ||
| $this->size = filesize($attachment->path); | ||
| $this->end = $this->size - 1; | ||
|
|
||
| header("Accept-Ranges: 0-".$this->end); | ||
| header("Accept-Ranges: 0-" . $this->end); | ||
| //set header | ||
| if (isset($_SERVER['HTTP_RANGE'])) { | ||
|
|
||
|
|
@@ -335,7 +405,7 @@ public function stream($id, $name = null) | |
| } | ||
| if ($range == '-') { | ||
| $c_start = $this->size - substr($range, 1); | ||
| }else{ | ||
| } else { | ||
| $range = explode('-', $range); | ||
| $c_start = $range[0]; | ||
| $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end; | ||
|
|
@@ -351,19 +421,17 @@ public function stream($id, $name = null) | |
| $length = $this->end - $this->start + 1; | ||
| fseek($this->stream, $this->start); | ||
| header('HTTP/1.1 206 Partial Content'); | ||
| header("Content-Length: ".$length); | ||
| header("Content-Range: bytes $this->start-$this->end/".$this->size); | ||
| } | ||
| else | ||
| { | ||
| header("Content-Length: ".$this->size); | ||
| header("Content-Length: " . $length); | ||
| header("Content-Range: bytes $this->start-$this->end/" . $this->size); | ||
| } else { | ||
| header("Content-Length: " . $this->size); | ||
| } | ||
| //stream | ||
| $i = $this->start; | ||
| set_time_limit(0); | ||
| while(!feof($this->stream) && $i <= $this->end) { | ||
| while (!feof($this->stream) && $i <= $this->end) { | ||
| $bytesToRead = $this->buffer; | ||
| if(($i+$bytesToRead) > $this->end) { | ||
| if (($i + $bytesToRead) > $this->end) { | ||
| $bytesToRead = $this->end - $i + 1; | ||
| } | ||
| $data = @stream_get_contents($this->stream, $bytesToRead, intval($i)); | ||
|
|
@@ -389,14 +457,13 @@ public function editImage($id) | |
| if ($this->Attachments->replaceFile($id, $tempPath)) { | ||
| $this->Flash->success(__('Image modified.')); | ||
| $redirectTo = $this->getRequest()->getSession()->consume('Attachment.redirectAfter'); | ||
| if($redirectTo) { | ||
| if ($redirectTo) { | ||
| return $this->redirect($redirectTo); | ||
| } | ||
| } else { | ||
| $this->Flash->error(__('Image could not be saved. Please, try again.')); | ||
| } | ||
| } | ||
| else { | ||
| } else { | ||
| $this->getRequest()->getSession()->write('Attachment.redirectAfter', $this->referer()); | ||
| } | ||
| $this->set('image', $image); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.