Example #1
0
 public function testFile()
 {
     $file = \Coast\File::createTemp();
     $this->_xml->writeFile($file);
     $file->open('r');
     $this->assertEquals($this->_output, $file->read());
     $file->close();
     $file->remove();
 }
Example #2
0
 public function move(\Coast\File $file)
 {
     if (!$file->exists()) {
         throw new \Exception("File '{$file}' does not exist");
     }
     $this->remove();
     $this->name(ucwords(trim(preg_replace('/[_]+/', ' ', $file->fileName()))));
     $this->size = $file->size();
     $this->hash = $file->hash('md5');
     $extName = strtolower($file->extName());
     if ($extName) {
         foreach (self::$_mimeTypes as $mimeType => $info) {
             if (in_array($extName, $info[1])) {
                 $this->subtype = $mimeType;
                 break;
             }
         }
     }
     if (!isset($this->subtype)) {
         $this->subtype = 'application/octet-stream';
     }
     $regex = '/[\\/\\?<>\\:\\*\\|":\\x00-\\x1f\\x80-\\x9f]/';
     $sanitized = clone $file;
     $sanitized->baseName(preg_replace($regex, '', $file->baseName()));
     $i = 0;
     $dir = $this->dir();
     do {
         $temp = clone $sanitized;
         if ($i > 0) {
             $temp->suffix("-{$i}");
         }
         $i++;
     } while ($dir->file($temp->baseName())->exists());
     $this->baseName($temp->baseName());
     $this->file = $file->move($dir, $this->baseName);
     return $this;
 }
Example #3
0
function is_image(\Coast\File $file)
{
    $exts = ['gif', 'jpg', 'jpeg', 'png', 'webp'];
    return in_array($file->extName(), $exts);
}
Example #4
0
 protected function _generateOutput(File $file, $transforms, array $params)
 {
     sort($transforms);
     ksort($params);
     $id = md5($file->name() . $file->modifyTime()->getTimestamp() . serialize($transforms) . serialize($params));
     return $this->_outputDir->dir("{$id[0]}/{$id[1]}", true)->file("{$id}.{$file->extName()}");
 }
Example #5
0
 public function file(File $file, $type, $download = false, $name = null)
 {
     if ($name === true) {
         $name = $file->baseName();
     }
     $this->_body = $file;
     $this->type($type)->header('Cache-Control', "public")->header('Content-Length', $file->size());
     if ($download) {
         $this->header('Content-Disposition', "attachment" . (isset($name) ? "; filename={$name}" : null));
     }
     return $this;
 }