/** * Handles an UploadedFile from form input. Stores, creates a model, and generates a thumbnail. * * @param UploadedFile $upload * @return FileStorage */ public static function storeUpload(UploadedFile $upload) { $fileContent = File::get($upload); $fileMD5 = md5((string) File::get($upload)); $storage = static::getHash($fileMD5); if (!$storage instanceof static) { $storage = new static(); $fileTime = $storage->freshTimestamp(); $storage->hash = $fileMD5; $storage->banned = false; $storage->filesize = $upload->getSize(); $storage->mime = $upload->getClientMimeType(); $storage->first_uploaded_at = $fileTime; $storage->upload_count = 0; if (!isset($upload->case)) { $ext = $upload->guessExtension(); $upload->case = Sleuth::check($upload->getRealPath(), $ext); if (!$upload->case) { $upload->case = Sleuth::check($upload->getRealPath()); } } if (is_object($upload->case)) { $storage->mime = $upload->case->getMimeType(); if ($upload->case->getMetaData()) { $storage->meta = json_encode($upload->case->getMetaData()); } } } else { $fileTime = $storage->freshTimestamp(); } if (!Storage::exists($storage->getPath())) { Storage::put($storage->getPath(), $fileContent); Storage::makeDirectory($storage->getDirectoryThumb()); } $storage->processAttachment(); return $storage; }
/** * Handles an UploadedFile from form input. Stores, creates a model, and generates a thumbnail. * * @static * @param UploadedFile|File $file * @return FileStorage */ public static function storeUpload($file) { $clientUpload = false; if (!$file instanceof SymfonyFile && !$file instanceof UploadedFile) { throw new \InvalidArgumentException("First argument for FileStorage::storeUpload is not a File or UploadedFile."); return false; } else { if ($file instanceof UploadedFile) { $clientUpload = true; } } $fileContent = File::get($file); $fileMD5 = md5((string) File::get($file)); $storage = static::getHash($fileMD5); if (!$storage instanceof static) { $storage = new static(); $fileTime = $storage->freshTimestamp(); $storage->hash = $fileMD5; $storage->banned = false; $storage->filesize = $file->getSize(); $storage->mime = $clientUpload ? $file->getClientMimeType() : $file->getMimeType(); $storage->first_uploaded_at = $fileTime; $storage->upload_count = 0; if (!isset($file->case)) { $ext = $file->guessExtension(); $file->case = Sleuth::check($file->getRealPath(), $ext); if (!$file->case) { $file->case = Sleuth::check($file->getRealPath()); } } if (is_object($file->case)) { $storage->mime = $file->case->getMimeType(); if ($file->case->getMetaData()) { $storage->meta = json_encode($file->case->getMetaData()); } } } else { $fileTime = $storage->freshTimestamp(); } if (!Storage::exists($storage->getPath())) { Storage::put($storage->getPath(), $fileContent); Storage::makeDirectory($storage->getDirectoryThumb()); } $storage->processAttachment(); return $storage; }