Beispiel #1
0
 /**
  * Gets all of the attachments for the given statements.
  * @param [\stdClass] $statements
  * @param IndexOptions $opts
  * @return [\stdClass]
  */
 public function index(array $statements, IndexOptions $opts)
 {
     $dir = $this->getDir($opts);
     $attachments = [];
     foreach ($statements as $statement) {
         $attachments = array_merge($attachments, array_map(function ($attachment) use($dir) {
             $ext = $this->getExt($attachment->contentType);
             $filename = $attachment->sha2 . '.' . $ext;
             return (object) ['content_type' => $attachment->contentType, 'hash' => $attachment->sha2, 'content' => FileFactory::create()->stream($dir . $filename, [])];
         }, isset($statement->attachments) ? $statement->attachments : []));
     }
     return $attachments;
 }
 public function fire()
 {
     $from_var = $this->option('from');
     $to_var = $this->argument('to');
     $from_repo = FileFactory::createRepo($from_var);
     $to_repo = FileFactory::createRepo($to_var);
     $files = $from_repo->index([]);
     $count = 0;
     foreach ($files as $file) {
         $path = $file['path'];
         if ($file['type'] === 'file' && !$to_repo->has($path, [])) {
             $count += 1;
             $to_repo->update($path, ['content' => $from_repo->show($path, [])], []);
             echo "Migrated '{$path}' from '{$from_var}' to '{$to_var}'." . PHP_EOL;
         }
     }
     echo "Migrated {$count} files and ignored " . (count($files) - $count) . "." . PHP_EOL;
 }
 /**
  * Generates content response.
  * @param mixed $data used to select the Document.
  * @return Response
  */
 public function documentResponse($data)
 {
     $document = $this->document->find($this->getOptions(), $this->document_type, $data);
     if (!$document) {
         throw new Exceptions\NotFound($data[$this->identifier], $this->document_type);
     } else {
         $headers = ['Updated' => $document->updated_at->toISO8601String(), 'Content-Type' => $document->contentType, 'ETag' => '"' . $document->sha . '"'];
         if ($this->method === 'HEAD') {
             //Only return headers
             return \Response::make(null, 200, $headers);
         } else {
             switch ($document->contentType) {
                 case "application/json":
                     return \Response::json($document->content, 200, $headers);
                 case "text/plain":
                     return \Response::make($document->content, 200, $headers);
                 default:
                     $stream = FileFactory::create()->stream($document->getFilePath(), []);
                     return \Response::stream(function () use($stream) {
                         while (!feof($stream)) {
                             echo fread($stream, 8192);
                             flush();
                             ob_flush();
                         }
                         fclose($stream);
                     }, 200, $headers);
             }
         }
     }
 }
Beispiel #4
0
 private function saveDocument($content, $contentType)
 {
     $dir = $this->getContentDir();
     if ($content instanceof UploadedFile) {
         $origname = $content->getClientOriginalName();
         $parts = pathinfo($origname);
         $filename = Str::slug(Str::lower($parts['filename'])) . '-' . time() . '.' . $parts['extension'];
         // Stores the file in a temporary location before writing it to the FileRepository.
         $tmp_location = __DIR__ . '/../../uploads/tmp';
         $content->move($tmp_location, $filename);
         $data = file_get_contents($tmp_location . '/' . $filename);
         FileFactory::create()->update($dir . $filename, ['content' => $data], []);
         unlink($tmp_location . '/' . $filename);
     } else {
         $ext = array_search($contentType, FileTypes::getMap());
         $filename = time() . '_' . mt_rand(0, 1000) . ($ext !== false ? '.' . $ext : '');
         $size = FileFactory::create()->update($dir . $filename, ['content' => $content], []);
         if ($size === false) {
             throw new Exceptions\Exception('There was an issue saving the content');
         }
     }
     $this->content = $filename;
     $this->setSha($content);
 }