/**
  * Create track from local hard drive with job service
  * 
  * @param MultimediaObject $multimediaObject
  * @param UploadedFile $file
  * @param string $profile
  * @param int $priority
  * @param string $language
  * @param array $description
  * @return MultimediaObject
  */
 public function createTrackFromLocalHardDrive(MultimediaObject $multimediaObject, UploadedFile $trackFile, $profile, $priority, $language, $description)
 {
     if (null === $this->profileService->getProfile($profile)) {
         throw new \Exception("Can't find given profile with name " . $profile);
     }
     if (UPLOAD_ERR_OK != $trackFile->getError()) {
         throw new \Exception($trackFile->getErrorMessage());
     }
     if (!is_file($trackFile->getPathname())) {
         throw new FileNotFoundException($trackFile->getPathname());
     }
     $pathFile = $trackFile->move($this->tmpPath . "/" . $multimediaObject->getId(), $trackFile->getClientOriginalName());
     $this->jobService->addJob($pathFile, $profile, $priority, $multimediaObject, $language, $description);
     return $multimediaObject;
 }
 public function upload(UploadedFile $file, $filename, $headers = array())
 {
     $adapter = $this->filesystem->getAdapter();
     $adapter->setMetadata($filename, $headers);
     $adapter->write($filename, file_get_contents($file->getPathname()));
     @unlink($file->getPathname());
     return $filename;
 }
Exemple #3
0
 /**
  * Save file to folder.
  *
  * @author Casper Rasmussen <*****@*****.**>
  *
  * @param  \Symfony\Component\HttpFoundation\File\UploadedFile $uploadedFile
  * @param  \Nodes\Assets\Upload\Settings                       $settings
  *
  * @return string
  * @throws \Nodes\Assets\Upload\Exceptions\AssetsUploadFailedException
  */
 protected function store(UploadedFile $uploadedFile, Settings $settings)
 {
     // Fallback folder is none is set
     if (!$settings->hasFolder()) {
         $settings->setFolder('default');
     }
     try {
         // Retrieve folder path
         $path = public_path(config('nodes.assets.providers.publicFolder.subFolder')) . DIRECTORY_SEPARATOR . $settings->getFolder();
         // If folder doesn't exists,
         // we'll create it with global permissions
         if (!file_exists($path)) {
             mkdir($path, 0777, true);
         }
         // Stream uploaded file
         $content = file_get_contents($uploadedFile->getPathname());
         // Save uploaded file to folder
         $result = file_put_contents($path . DIRECTORY_SEPARATOR . $settings->getFileName() . '.' . $settings->getFileExtension(), $content);
         if (!$result) {
             throw new NodesException('Failed to save', 500);
         }
     } catch (Exception $e) {
         throw new AssetsUploadFailedException('Could not save the file to public folder. Reason: ' . $e->getMessage());
     }
     return $settings->getFilePath();
 }
 /**
  * Set a pic from an url into the event
  */
 public function addPicFile(Event $event, UploadedFile $picFile)
 {
     if (UPLOAD_ERR_OK != $picFile->getError()) {
         throw new \Exception($picFile->getErrorMessage());
     }
     if (!is_file($picFile->getPathname())) {
         throw new FileNotFoundException($picFile->getPathname());
     }
     $path = $picFile->move($this->targetPath . "/" . $event->getId(), $picFile->getClientOriginalName());
     $pic = new Pic();
     $pic->setUrl(str_replace($this->targetPath, $this->targetUrl, $path));
     $pic->setPath($path);
     $event->setPic($pic);
     $this->dm->persist($event);
     $this->dm->flush();
     return $event;
 }
Exemple #5
0
 public static function upload(UploadedFile $file, $user)
 {
     $userId = $user;
     if ($user instanceof User) {
         $userId = $user->id;
     }
     $hash = md5_file($file->getPathname());
     $image = Image::whereHash($hash)->whereUploadedBy($userId)->first();
     if ($image) {
         return $image;
     }
     $image = new Image();
     try {
         $image->uploaded_by = $userId;
         $image->size = $file->getSize();
         $image->filename = $file->getClientOriginalName();
         $image->extension = $file->getClientOriginalExtension();
         $image->mime = $file->getMimeType();
         $image->hash = $hash;
         $image->save();
         $image->ensureDirectoryExists();
         foreach (self::$ImageTypes as $coverType) {
             if ($coverType['id'] === self::ORIGINAL && $image->mime === 'image/jpeg') {
                 $command = 'cp ' . $file->getPathname() . ' ' . $image->getFile($coverType['id']);
             } else {
                 // ImageMagick options reference: http://www.imagemagick.org/script/command-line-options.php
                 $command = 'convert 2>&1 "' . $file->getPathname() . '" -background white -alpha remove -alpha off -strip';
                 if ($image->mime === 'image/jpeg') {
                     $command .= ' -quality 100 -format jpeg';
                 } else {
                     $command .= ' -quality 95 -format png';
                 }
                 if (isset($coverType['width']) && isset($coverType['height'])) {
                     $command .= " -thumbnail {$coverType['width']}x{$coverType['height']}^ -gravity center -extent {$coverType['width']}x{$coverType['height']}";
                 }
                 $command .= ' "' . $image->getFile($coverType['id']) . '"';
             }
             External::execute($command);
             chmod($image->getFile($coverType['id']), 0644);
         }
         return $image;
     } catch (\Exception $e) {
         $image->delete();
         throw $e;
     }
 }
Exemple #6
0
 /**
  * @param UploadedFile $image
  * @param string $visibility
  * @return $this
  */
 public function store(UploadedFile $image, $visibility = Filesystem::VISIBILITY_PUBLIC)
 {
     $info = getimagesize($image->getPathname());
     $this->width = $info[0];
     $this->height = $info[1];
     $this->cloud->put($this->path, file_get_contents($image), $visibility);
     return $this;
 }
Exemple #7
0
 /**
  * Physically deletes the file
  * @return bool
  */
 public function remove()
 {
     if (file_exists($this->photoUpload->getPathname())) {
         unlink($this->photoUpload->getPathname());
         return true;
     }
     return false;
 }
 /**
  * {@inheritDoc}
  */
 protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, $name)
 {
     $filesystem = $this->getFilesystem($mapping);
     $path = !empty($dir) ? $dir . '/' . $name : $name;
     if ($filesystem->getAdapter() instanceof MetadataSupporter) {
         $filesystem->getAdapter()->setMetadata($path, array('contentType' => $file->getMimeType()));
     }
     $filesystem->write($path, file_get_contents($file->getPathname()), true);
 }
 /**
  * Add a material from a file into the multimediaObject
  */
 public function addMaterialFile(MultimediaObject $multimediaObject, UploadedFile $materialFile, $formData)
 {
     if (UPLOAD_ERR_OK != $materialFile->getError()) {
         throw new \Exception($materialFile->getErrorMessage());
     }
     if (!is_file($materialFile->getPathname())) {
         throw new FileNotFoundException($materialFile->getPathname());
     }
     $material = new Material();
     $material = $this->saveFormData($material, $formData);
     $path = $materialFile->move($this->targetPath . "/" . $multimediaObject->getId(), $materialFile->getClientOriginalName());
     $material->setPath($path);
     $material->setUrl(str_replace($this->targetPath, $this->targetUrl, $path));
     $multimediaObject->addMaterial($material);
     $this->dm->persist($multimediaObject);
     $this->dm->flush();
     return $multimediaObject;
 }
 public function upload(UploadedFile $file)
 {
     if (!in_array($file->getMimeType(), $this->allowedMimeTypes)) {
         throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $file->getMimeType()));
     }
     $filename = sprintf('%s/%s/%s/%s.%s', date('Y'), date('m'), date('d'), uniqid(), $file->getClientOriginalExtension());
     $adapter = $this->filesystem->getAdapter();
     $adapter->setMetadata($filename, array('contentType' => $file->getMimeType()));
     $adapter->write($filename, file_get_contents($file->getPathname()));
     return $adapter->getUrl($filename);
 }
 public function upload(UploadedFile $file, $path)
 {
     // Check if the file's mime type is in the list of allowed mime types.
     if (!in_array($file->getClientMimeType(), self::$allowedMimeTypes)) {
         throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $file->getClientMimeType()));
     }
     $adapter = $this->filesystem->getAdapter();
     $adapter->setMetadata($path, array('contentType' => $file->getClientMimeType()));
     $adapter->write($path, file_get_contents($file->getPathname()));
     return $path;
 }
 protected function uploadFile(UploadedFile $file)
 {
     $web_path = '/uploads/fc_request/' . date('Y/m/d');
     $directory = rtrim(__DIR__, '/') . '/../../../../../../../../../web' . $web_path;
     $name = $file->getClientOriginalName();
     @mkdir($directory, 0777, true);
     if (!@copy($file->getPathname(), $directory . '/' . $name)) {
         return null;
     }
     return $web_path . '/' . $name;
 }
Exemple #13
0
 /**
  * @param UploadedFile $uploadedFile
  * @param FileEntity   $fileEntity
  * @return bool
  */
 public function putUploadedFile(UploadedFile $uploadedFile, FileEntity $fileEntity)
 {
     list($path, $filesystem, $settings) = $this->getConfigValuesForRecordType($fileEntity->getRecordType());
     $fullPath = $path . DIRECTORY_SEPARATOR . $fileEntity->getFileName();
     $this->getFilesystem($filesystem)->putStream($fullPath, fopen($uploadedFile->getPathname(), 'r'), $settings);
     $fileEntity->setContentType($uploadedFile->getMimeType())->setSize($uploadedFile->getSize());
     /** @var EntityManager $em */
     $em = $this->container['orm.em'];
     $em->flush();
     return $fileEntity;
 }
Exemple #14
0
 /**
  * Resize user avatar from uploaded picture
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile|\Symfony\Component\HttpFoundation\File\File $original
  * @param int $user_id
  * @param string $size
  * @throws \Exception
  * @return null
  */
 public function resizeAndSave($original, $user_id, $size = 'small')
 {
     $sizeConvert = ['big' => [400, 400], 'medium' => [200, 200], 'small' => [100, 100]];
     if (!array_key_exists($size, $sizeConvert)) {
         return null;
     }
     $image = new Image();
     $image->setCacheDir(root . '/Private/Cache/images');
     $image->open($original->getPathname())->cropResize($sizeConvert[$size][0], $sizeConvert[$size][1])->save(root . '/upload/user/avatar/' . $size . '/' . $user_id . '.jpg', 'jpg', static::COMPRESS_QUALITY);
     return null;
 }
Exemple #15
0
 public function uploadLicenceFile()
 {
     if (!$this->hasLicenceFile()) {
         return false;
     }
     $licencePath = $this->getAbsoluteDir() . '/serverkey.dat';
     $filepath = $this->licenceFile->getPathname();
     $content = file_get_contents($filepath);
     @unlink($filepath);
     unset($this->licenceFile);
     return $this->getMachine()->getConnection()->upload($licencePath, $content, 0750);
 }
Exemple #16
0
 /**
  * Build a \Torann\MediaSort\File\UploadedFile object from
  * a Symfony\Component\HttpFoundation\File\UploadedFile object.
  *
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
  *
  * @return \Torann\MediaSort\File\UploadedFile
  * @throws \Torann\MediaSort\Exceptions\FileException
  */
 protected function createFromObject(SymfonyUploadedFile $file)
 {
     $path = $file->getPathname();
     $originalName = $file->getClientOriginalName();
     $mimeType = $file->getClientMimeType();
     $size = $file->getClientSize();
     $error = $file->getError();
     $uploadFile = new UploadedFile($path, $originalName, $mimeType, $size, $error);
     if (!$uploadFile->isValid()) {
         throw new FileException($uploadFile->getErrorMessage($uploadFile->getError()));
     }
     return $uploadFile;
 }
 /**
  * Set a pic from an url into the series
  */
 public function addPicFile(Series $series, UploadedFile $picFile, $isBanner = false, $bannerTargetUrl = "")
 {
     if (UPLOAD_ERR_OK != $picFile->getError()) {
         throw new \Exception($picFile->getErrorMessage());
     }
     if (!is_file($picFile->getPathname())) {
         throw new FileNotFoundException($picFile->getPathname());
     }
     $path = $picFile->move($this->targetPath . "/" . $series->getId(), $picFile->getClientOriginalName());
     $pic = new Pic();
     $pic->setUrl(str_replace($this->targetPath, $this->targetUrl, $path));
     $pic->setPath($path);
     if ($isBanner) {
         $pic->setHide(true);
         $pic->addTag('banner');
         $series = $this->addBanner($series, $pic->getUrl(), $bannerTargetUrl);
     }
     // TODO: add pic the latest if it is banner
     $series->addPic($pic);
     $this->dm->persist($series);
     $this->dm->flush();
     return $series;
 }
 /**
  * {@inheritDoc}
  */
 protected function doUpload(UploadedFile $file, $dir, $name)
 {
     $adapter = $this->getAdapter($dir);
     $src = new Local($file->getPathname());
     $dst = $adapter->createFileStream($name);
     $src->open(new StreamMode('rb+'));
     $dst->open(new StreamMode('ab+'));
     while (!$src->eof()) {
         $data = $src->read(100000);
         $written = $dst->write($data);
     }
     $dst->close();
     $src->close();
 }
Exemple #19
0
 protected function appendData($filePathPartial, UploadedFile $file)
 {
     if (!($out = @fopen($filePathPartial, "ab"))) {
         throw new Exception("Failed to open output stream.", 102);
     }
     if (!($in = @fopen($file->getPathname(), "rb"))) {
         throw new Exception("Failed to open input stream", 101);
     }
     while ($buff = fread($in, 4096)) {
         fwrite($out, $buff);
     }
     @fclose($out);
     @fclose($in);
 }
 /**
  * Upload method
  *
  * @param UploadedFile $file file to upload
  *
  * @return string
  * @throws \InvalidArgumentException
  */
 public function upload(UploadedFile $file)
 {
     $fileMimeType = $file->getClientMimeType();
     if ($this->allowedTypes && !in_array($fileMimeType, $this->allowedTypes)) {
         throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $fileMimeType));
     }
     $filename = $this->generateNameByOriginal($file->getClientOriginalName());
     $adapter = $this->filesystem->getAdapter();
     if (!$adapter instanceof Local) {
         $adapter->setMetadata($filename, ['contentType' => $fileMimeType]);
     }
     $adapter->write($filename, file_get_contents($file->getPathname()));
     return $filename;
 }
 /**
  * Accepts an uploaded file and fills out the details into an Attachment object.
  *
  * @param UploadedFile $file
  * @param AttachmentInterface $attachment
  * @return \Symfony\Component\HttpFoundation\File\File
  */
 public function acceptUpload(UploadedFile $file, AttachmentInterface $attachment)
 {
     $hash = sha1_file($file->getPathname());
     $name = $this->pathHelper->getName($file, $attachment, $hash);
     $filename = $this->sanitiser->sanitiseFilename($file->getClientOriginalName());
     $mimeType = $this->sanitiser->sanitiseMimeType($file->getClientMimeType());
     $attachment->setFilename($filename);
     $attachment->setFileHash($hash);
     $attachment->setFileSize(filesize($file->getPathname()));
     $attachment->setMimeType($mimeType);
     $attachment->setPhysicalName($name);
     $fullPath = sprintf('%s/%s', $this->pathHelper->getSaveDir($attachment), $name);
     return $file->move(dirname($fullPath), basename($fullPath));
 }
Exemple #22
0
 private function appendData($filePathPartial, UploadedFile $file)
 {
     if (!($out = @fopen($filePathPartial, 'ab'))) {
         throw new PluploadException('Failed to open output stream.', 102);
     }
     if (!($in = @fopen($file->getPathname(), 'rb'))) {
         throw new PluploadException('Failed to open input stream', 101);
     }
     while ($buff = fread($in, 4096)) {
         fwrite($out, $buff);
     }
     @fclose($out);
     @fclose($in);
 }
Exemple #23
0
 /**
  * {@inheritdoc}
  */
 public function canUpload(UploadedFile $file)
 {
     $mimeType = $file->getMimeType();
     if (in_array($mimeType, $this->getSupportedMimeTypes())) {
         return 5;
     }
     if ($file->getMimeType() == 'application/ogg') {
         // This could be a video or audio file.
         $meta = GenericMetadataReader::readMetadata($file->getPathname());
         if (isset($meta['audio']['dataformat'])) {
             return 5;
         }
     }
     return 0;
 }
 /**
  * generate image name
  *
  * @param  UploadedFile $imageFile
  * @param  string $prefix
  * @return array
  */
 protected function moveImage($imageFile, $prefix)
 {
     // destinationName
     $destinationPath = public_path('resource\\' . $prefix);
     // checkImage and get type
     $type = $this->checkImage($imageFile->getPathname());
     // generate image name
     $imageName = $this->generateImageName($prefix, $type);
     // move image to destination
     $movedImageFile = $imageFile->move($destinationPath, $imageName);
     // get movedImageFilePathname
     $movedImagePathname = $movedImageFile->getPathname();
     // url
     $movedImageUrl = url('resource/' . "{$prefix}/" . $imageName);
     return ['imgPathname' => $movedImagePathname, 'imgUrl' => $movedImageUrl];
 }
 public function upload(UploadedFile $file, $path = null)
 {
     // Check if the file's mime type is in the list of allowed mime types.
     if (!in_array($file->getClientMimeType(), self::$allowedMimeTypes)) {
         throw new \InvalidArgumentException(sprintf('Files of type %s are not allowed.', $file->getClientMimeType()));
     }
     if ($path) {
         $filename = sprintf('%s/%s.%s', $path, md5(time() * rand(0, 99)), $file->getClientOriginalExtension());
     } else {
         $filename = sprintf('%s/%s/%s/%s.%s', date('Y'), date('m'), date('d'), md5(time() * rand(0, 99)), $file->getClientOriginalExtension());
     }
     $adapter = $this->filesystem->getAdapter();
     $adapter->setMetadata($filename, array('contentType' => $file->getClientMimeType()));
     $adapter->write($filename, file_get_contents($file->getPathname()));
     return $filename;
 }
Exemple #26
0
 /**
  * Upload the photo
  *
  * @return boolean
  */
 public function upload()
 {
     $rootDir = __DIR__ . '/../../../../../web/uploads/apps';
     /*
      * Upload and resize
      */
     $imagine = new Imagine();
     // Create the logo thumbnail in a 200x200 box
     if (null === $this->file) {
         $thumbnail = $imagine->open($rootDir . '/default.png')->thumbnail(new Box(200, 200), Image::THUMBNAIL_OUTBOUND);
     } else {
         $thumbnail = $imagine->open($this->file->getPathname())->thumbnail(new Box(200, 200), Image::THUMBNAIL_OUTBOUND);
     }
     // Save the result
     $thumbnail->save($rootDir . '/' . $this->getClientId() . '.png');
 }
Exemple #27
0
 /**
  * {@inheritDoc}
  */
 protected function doUpload(PropertyMapping $mapping, UploadedFile $file, $dir, $name)
 {
     $filesystem = $this->getFilesystem($mapping);
     if ($filesystem->getAdapter() instanceof MetadataSupporter) {
         $filesystem->getAdapter()->setMetadata($name, array('contentType' => $file->getMimeType()));
     }
     $src = new LocalStream($file->getPathname());
     $dst = $filesystem->createStream($dir . $name);
     $src->open(new StreamMode('rb+'));
     $dst->open(new StreamMode('wb+'));
     while (!$src->eof()) {
         $data = $src->read(100000);
         $dst->write($data);
     }
     $dst->close();
     $src->close();
 }
 /**
  * @param int  $blur
  * @param bool $darken
  *
  * @return File
  */
 private function makeBackgroundImage($blur = 10, $darken = true)
 {
     $srcPath = $this->file->getPathname();
     $dstFile = $this->makeTmpFile();
     $dstPath = $dstFile->getPathname();
     $cmd = "convert {$srcPath} -resize 1280 -gravity center";
     if ($blur) {
         $cmd .= " -blur {$blur},{$blur} ";
     }
     if ($darken) {
         $cmd .= " -fill black -colorize 20%";
     }
     $cmd .= " {$dstPath} 2>&1";
     shell_exec($cmd);
     $this->optimizeJpg($dstFile);
     return $dstFile;
 }
Exemple #29
0
 /**
  * File upload callback
  * Resizes the image according to validation constraints
  *
  * @param string                                             $field Field name
  * @param Symfony\Component\HttpFoundation\File\UploadedFile $file  File to process
  *
  * @return void
  */
 protected function imageUpload($field, UploadedFile $file)
 {
     if (UPLOAD_ERR_OK == $file->getError()) {
         $asserts = $this->getAsserts()[$field];
         foreach ($asserts as $assert) {
             // Detect image size constraints and resize accordingly
             if ($assert instanceof Image) {
                 $path = $file->getPathname();
                 $pathWithExtension = $path . '.' . $file->guessExtension();
                 rename($path, $pathWithExtension);
                 // ImageWorkshop relies on the file's extension for encoding
                 try {
                     $this->resize($pathWithExtension, $assert->maxWidth, $assert->maxHeight);
                 } catch (ImageWorkshopException $e) {
                 }
                 rename($pathWithExtension, $path);
             }
         }
     }
 }
Exemple #30
-1
 /**
  * Upload provided file and create matching FileRecord object
  *
  * @param UploadedFile $file
  * @param $clientIp
  * @return FileRecord
  */
 public function uploadFile(UploadedFile $file, $clientIp)
 {
     $extension = Str::lower($file->getClientOriginalExtension());
     $generatedName = $this->generateName($extension);
     // Create SHA-256 hash of file
     $fileHash = hash_file('sha256', $file->getPathname());
     // Check if file already exists
     $existingFile = FileRecord::where('hash', '=', $fileHash)->first();
     if ($existingFile) {
         return $existingFile;
     }
     // Query previous scans in VirusTotal for this file
     if (config('virustotal.enabled') === true) {
         $this->checkVirusTotalForHash($fileHash);
     }
     // Get filesize
     $filesize = $file->getSize();
     // Check max upload size
     $maxUploadSize = config('upload.max_size');
     if ($filesize > $maxUploadSize) {
         throw new MaxUploadSizeException();
     }
     // Move the file
     $uploadDirectory = config('upload.directory');
     $file->move($uploadDirectory, $generatedName);
     // Create the record
     /** @var FileRecord $record */
     $record = FileRecord::create(['client_name' => $file->getClientOriginalName(), 'generated_name' => $generatedName, 'filesize' => $filesize, 'hash' => $fileHash, 'uploaded_by_ip' => $clientIp]);
     return $record;
 }