public function upload($request) { if (!$request->headers->has('x-filename')) { return new Response('Filename is missing (header X-FILENAME)', 400); } $filename = $request->headers->get('x-filename'); $pathinfo = pathinfo($filename); $dpath = Utils::resurl($this->draftPath . '/files', $pathinfo['filename'], null, null, $pathinfo['extension']); if (!$this->isSafeExtension($pathinfo['extension'])) { return $this->json($request, array('status' => 1, 'errMessage' => 'Forbidden file extension ' . $pathinfo['extension']), 200); } $res = $this->fs->writeStream($dpath, fopen("php://input", 'r')); if ($res === false) { return $this->json($request, array('status' => 1, 'errMessage' => 'Unable to upload file ' . $pathinfo['filename'] . '.' . $pathinfo['extension']), 200); } else { return $this->json($request, array('status' => 0, 'url' => $res), 200); } }
protected function generateImageSet($img, $filename, $ext) { $width = $img->getWidth(); $ratio = $width / $img->getHeight(); $widths = $this->ctx['image.srcset_widths']; $maxDiff = $this->ctx['image.srcset_width_maxdiff']; rsort($widths); $maxWidth = $widths[0]; if ($width > $maxWidth) { $width = $maxWidth; } $id = uniqid(); $srcset = array(); foreach ($this->neededWidths($width, $widths, $maxDiff) as $targetWidth) { $tpath = Utils::resurl($this->imgDir(), $filename, $id, '-' . $targetWidth, $ext); $timg = $img->resize($targetWidth); $targetHeight = $timg->getHeight(); $this->ctx['fs']->write($tpath, $timg->asString($ext)); unset($timg); array_push($srcset, array('width' => $targetWidth, 'height' => $targetHeight, 'url' => $tpath)); } return array('srcset' => $srcset, 'ratio' => $ratio); }
function test_resurl() { $this->assertTrue(preg_match('/^name\\-sc[0-9a-f]{13}\\.ext$/', Utils::resurl('name.ext')) === 1); $this->assertTrue(preg_match('/^\\/name\\-sc[0-9a-f]{13}\\.ext$/', Utils::resurl('/name.eXt')) === 1); $this->assertTrue(preg_match('/^na\\-m\\.e\\-sc[0-9a-f]{13}\\.ext$/', Utils::resurl('na-m.e.ext')) === 1); $this->assertTrue(preg_match('/^path\\/name\\-sc[0-9a-f]{13}\\.ext$/', Utils::resurl('path/name.ext')) === 1); $this->assertEquals('path/name-sc1234567890123-sub.ext', Utils::resurl('path', 'name', '1234567890123', '-sub', 'ext')); $this->assertEquals('name-sc1234567890123-sub.ext', Utils::resurl('', 'name', '1234567890123', '-sub', 'ext')); $this->assertEquals('name-sc1234567890123.ext', Utils::resurl('', 'name', '1234567890123', null, 'ext')); }