createFromUploadedFile()

@author Tom Haskins-Vaughan <tom@harvestcloud.com>
@since  2013-11-23

@param  \Symfony\Component\HttpFoundation\File\UploadedFile $file

@return \HarvestCloud\CoreBundle\Entity\Image
433662
public static function createFromUploadedFile(UploadedFile $file)
    {
        $size = getimagesize($file->getRealPath());

        $image = new Image($file->guessExtension());
        $image->setOriginalFilename($file->getClientOriginalName());
        $image->setMimeType($file->getMimeType());
        $image->setSize(filesize($file->getRealPath()));
        $image->setWidth($size[0]);
        $image->setHeight($size[1]);

        return $image;
    }
221162
protected function handleUniqueFile($file)
    {
        $uploadedFile = null;

        //The current file was uploaded successfully?
        if (!$file['error']) {
            $uploadedFile = new UploadedFile(
                $file['tmp_name'],
                $file['name'],
                $file['type']
            );
        }

        return $uploadedFile;
    }
40292
public function upload(File $file, UploadedFile $upload, $contents)
    {
        $this->generateFilename($file);

        $method = 'write';

        if (is_resource($contents) && get_resource_type($contents) == 'stream') {
            $method = 'writeStream';
        }

        $meta = $this->adapter->{$method}($file->path, $contents, new Config());

        if (!$meta) {
            return false;
        }

        $this->meta = $meta;

        $this->generateUrl($file);

        return $file;
    }
653128
public function uploadOne(UploadedFile $file, $folder = null, $disk = 'public')
    {
        return $file->store($folder, ['disk' => $disk]);
    }
499953
public function file(UploadedFile $file)
	{
		$this->file = $file;
		$this->filename = $file->getClientOriginalName();
		$this->sanitizeFilename();

		return $this;
	}
604715
public function upload (UploadedFile $file, bool $full = null, string $id = null)
    {
        if (is_null ($file))
            return HCLog::info ('R-UPLOAD-001', trans ('resources::resources.errors.no_resource_selected'));

        DB::beginTransaction ();

        $this->resourceID = $id;

        try {
            $resource = $this->createResourceRecord ($file);
            $this->saveResourceInStorage ($resource, $file);

            //Generating checksum
            if ($resource['size'] <= env ('MAX_CHECKSUM_SIZE', self::MAX_CHECKSUM_SIZE))
                $resource->update (['checksum' => hash_file ('sha256', storage_path ('app/' . $resource['path']))]);

            Artisan::call ('hc:generate-thumbs', ['id' => $resource->id]);
        } catch (\Exception $e) {
            DB::rollback ();

            if (isset($resource))
                $this->removeImageFromStorage ($resource);

            throw new \Exception($e);
        }

        DB::commit ();

        if ($full)
            return $resource->toArray ();

        return [
            'id'  => $resource->id,
            'url' => route ('resource.get', $resource->id),
        ];
    }
430238
public function upload(UploadedFile $file = null)
    {
        if ($file === null) {
            return '';
        }

        $fileName = $this->getFileName($file);
        $file->move($this->getTargetDir(), $fileName);

        return $fileName;
    }
168503
public function actionUpload($type)
    {
        /** @var $loadFile \Symfony\Component\HttpFoundation\File\UploadedFile */
        $loadFile = App::$Request->files->get('upload');
        if ($loadFile === null || $loadFile->getError() !== 0)
            return $this->errorResponse(__('File upload failed'));

        // get file extension
        $fileExt = '.' . $loadFile->guessExtension();
        // check if this request type is allowed
        if ($this->allowedExt[$type] === null || !Any::isArray($this->allowedExt[$type]))
            throw new NativeException('Hack attempt');

        // check if this file extension is allowed to upload
        if (!Arr::in($fileExt, $this->allowedExt[$type])) {
            return $this->errorResponse(__('This file type is not allowed to upload'));
        }

        $date = Date::convertToDatetime(time(), 'd-m-Y');

        // create file hash based on name-size
        $fileNewName = App::$Security->simpleHash($loadFile->getFilename() . $loadFile->getSize()) . $fileExt;
        $savePath = Normalize::diskFullPath('/upload/' . $type . '/' . $date);
        // save file from tmp to regular
        $loadFile->move($savePath, $fileNewName);

        // generate URI of uploaded file
        $url = '/upload/' . $type . '/' . $date . '/' . $fileNewName;

        return App::$View->render('editor/load_success',
            [
                'callbackId' => (int)App::$Request->query->get('CKEditorFuncNum'),
                'url' => $url
            ],
            __DIR__);
    }
327005
protected function submit(UploadedFile $file, $type, array $attributes)
    {
        switch ($type) {
            case 'image':
                return (new \Routegroup\Media\Upload\Image)->make($file, $attributes);

            case 'audio':
                return (new \Routegroup\Media\Upload\Audio)->make($file, $attributes);

            case 'video':
                return (new \Routegroup\Media\Upload\Video)->make($file, $attributes);

            case 'archive':
                return (new \Routegroup\Media\Upload\Archive)->make($file, $attributes);

            default:
                return (new \Routegroup\Media\Upload\File)->make($file, $attributes);
        }
    }
738235
public function upload($file, $path='')
	{
		$path = ltrim($path, '/');
		$path = rtrim($path, '/');
		$basePath = $this->getImageBasePath();
		$imagePath = $basePath.'/'.$path;

		if (!File::exists($imagePath)) {
			throw new ImageUploaderException("Directory {$path} doesn't exists");
		}

		$uploadedFile = $this->getUploadedFile($file);
		$fileName = $this->generateFileName($uploadedFile->getClientOriginalExtension());
		$uploadedFile->move($imagePath, $fileName);

		if (!is_null($this->temporaryImage)) {
			File::delete($this->temporaryImage);
		}

		return $path.'/'.$fileName;
	}
157812
public function createUploadedFile(
        $file,
        $size = null,
        $error = \UPLOAD_ERR_OK,
        $clientFilename = null,
        $clientMediaType = null
    )
    {
        if ($size === null)
        {
            if (\is_string($file))
            {
                $size = (int)filesize($file);
            }
            else
            {
                $stats = \fstat($file);
                $size  = $stats['size'];
            }
        }

        return new UploadedFile($file, $size, $error, $clientFilename, $clientMediaType);
    }
729500
protected function uploadAndDeleteOriginal(UploadedFile $file)
    {
        $this->renameIfExists($file);

        $path = $this->storage->putFileAs($this->getDirectory(), $file, $this->name);

        $this->destroy();

        return $path;
    }
741922
protected function createFromArray($file)
	{
		$file = new SymfonyUploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);

		return $this->createFromObject($file);
	}
1268495
public static function createFromFile($file)
    {
        // If the file is not readable
        if (!is_readable($file)) {
            throw new InvalidArgumentException(
                sprintf(InvalidArgumentException::INVALID_FILE_STR, $file),
                InvalidArgumentException::INVALID_FILE
            );
        }

        return self::createFromString(file_get_contents($file));
    }
695212
public function createImage(UploadedFile $uploadedImage)
    {
        $image = $this->imageManager->create();
        $filename = $this->getUniqueFilename($uploadedImage);

        $image->setFilename($filename);
        $image->setMimeType($uploadedImage->getClientMimeType());
        $image->setOrderNr(1);
        $image->setOriginalPath($this->filesystem->getRelativeFilePath($filename));
        $image->setPath($image->getOriginalPath());
        $image->setSize($uploadedImage->getClientSize());
        $image->setTemporary(true);

        $this->imageManager->add($image);

        $fileDir = $this->filesystem->getActualFileDir($filename);

        $uploadedImage->move($fileDir, $image->getFilename());

        return $image;
    }
1344945
public function uploadFile(UploadedFile $file, $transformConfig = [], $appendedPath = null)
    {
        if ($file->isValid()) {
            return $this->upload(new SymfonyFile($file), $transformConfig, $appendedPath);
        }

        return false;
    }
1285446
private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
    {
        $temporaryPath = '';
        $clientFileName = '';
        if (UPLOAD_ERR_NO_FILE !== $psrUploadedFile->getError()) {
            $temporaryPath = $this->getTemporaryPath();
            $psrUploadedFile->moveTo($temporaryPath);

            $clientFileName = $psrUploadedFile->getClientFilename();
        }

        if (class_exists('Symfony\Component\HttpFoundation\HeaderUtils')) {
            // Symfony 4.1+
            return new UploadedFile(
                $temporaryPath,
                null === $clientFileName ? '' : $clientFileName,
                $psrUploadedFile->getClientMediaType(),
                $psrUploadedFile->getError(),
                true
            );
        }

        return new UploadedFile(
            $temporaryPath,
            null === $clientFileName ? '' : $clientFileName,
            $psrUploadedFile->getClientMediaType(),
            $psrUploadedFile->getSize(),
            $psrUploadedFile->getError(),
            true
        );
    }