Save attachment to file

@param int $attachmentID
@param string $destination directory or filename with path
@return int
778936
public static function saveToFile($attachmentID, $destination)
    {
        $result     = 0;
        $downloader = new Priloha($attachmentID);
        if ($downloader->lastResponseCode == 200) {

            $downloader->doCurlRequest(self::getDownloadURL($downloader), 'GET');
            if ($downloader->lastResponseCode == 200) {
                if (is_dir($destination)) {
                    $destination .= '/'.$downloader->getDataValue('nazSoub');
                }
                $result = file_put_contents($destination,
                    $downloader->lastCurlResponse);
            }
        }
        return $result;
    }
164691
public function move($destination) {
		$destination = $this->getDestination($destination);

		if (@rename($this->getPathname(), $destination)) {
			$this->pathname = $destination;
		} else {
			throw new FileException(sprintf('Failed to move %s to %s', $this->pathname, $destination));
		}
	}
1164909
public function image($destination, $min_resolution, $max_resolution) {
    $extension = pathinfo($destination, PATHINFO_EXTENSION);
    $min = explode('x', $min_resolution);
    $max = explode('x', $max_resolution);

    $width = rand((int) $min[0], (int) $max[0]);
    $height = rand((int) $min[1], (int) $max[1]);

    // Make an image split into 4 sections with random colors.
    $im = imagecreate($width, $height);
    for ($n = 0; $n < 4; $n++) {
      $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
      $x = $width / 2 * ($n % 2);
      $y = $height / 2 * (int) ($n >= 2);
      imagefilledrectangle($im, $x, $y, $x + $width / 2, $y + $height / 2, $color);
    }

    // Make a perfect circle in the image middle.
    $color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
    $smaller_dimension = min($width, $height);
    imageellipse($im, $width / 2, $height / 2, $smaller_dimension, $smaller_dimension, $color);

    $save_function = 'image' . ($extension == 'jpg' ? 'jpeg' : $extension);
    $save_function($im, $destination);
    return $destination;
  }
762232
protected function saveToFile(string $content, PhingFile $destination): void
    {
        $path = $destination->getPath();
        $this->log("Writing: $path", Project::MSG_INFO);

        (new FileWriter($destination))->write($content);
    }
1309937
public function saveFile($file, $contentId) {
    // Prepare directory
    if (empty($contentId)) {
      // Should be in editor tmp folder
      $path = $this->getEditorPath();
    }
    else {
      // Should be in content folder
      $path = $this->path . '/content/' . $contentId;
    }
    $path .= '/' . $file->getType() . 's';
    self::dirReady($path);

    // Add filename to path
    $path .= '/' . $file->getName();

    copy($_FILES['file']['tmp_name'], $path);

    return $file;
  }
1213594
protected function verifyAndSymlinkFile(string $source, string $destination, OutputInterface $output): void
    {
        if (!$this->fileSystem->exists(dirname($destination))) {
            $this->fileSystem->mkdir(dirname($destination), 0755);
        }

        if ($this->fileSystem->exists($destination) && !is_file($destination)) {
            $output->writeln('<comment>' . basename($destination) . '</comment> already exists in <comment>' . dirname($destination) . '/</comment> and is not a file/symlink. Skipping...');

            return;
        }

        if ($this->forceSymlinks && is_link($destination)) {
            $this->fileSystem->remove($destination);
        }

        if (is_file($destination) && !is_link($destination)) {
            if ($this->fileSystem->exists($destination . '.original')) {
                $output->writeln('Cannot create backup file <comment>' . basename($destination) . '.original</comment> in <comment>' . dirname($destination) . '/</comment>. Skipping...');

                return;
            }

            $this->fileSystem->rename($destination, $destination . '.original');
        }

        if ($this->fileSystem->exists($destination)) {
            if (is_link($destination)) {
                $output->writeln('Skipped creating the symlink for <comment>' . basename($destination) . '</comment> in <comment>' . dirname($destination) . '/</comment>. Symlink already exists!');
            } else {
                $output->writeln('Skipped creating the symlink for <comment>' . basename($destination) . '</comment> in <comment>' . dirname($destination) . '/</comment> due to an unknown error.');
            }

            return;
        }

        $this->fileSystem->symlink(
            $this->fileSystem->makePathRelative(
                dirname($source),
                realpath(dirname($destination))
            ) . basename($source),
            $destination
        );
    }
582012
protected function store( $source, $destination, $width, $height, $crop ) {
		if ( file_exists( $destination ) ) {
			return $destination;
		}

		$editor = wp_get_image_editor( $source );

		if ( is_wp_error( $editor ) ) {
			return '';
		}

		$editor->resize( $width, $height, $crop );
		$editor->save( $destination );

		return $destination;
	}
331133
public function copy($destination)
  {
    $f = $this->getValue();

    if ($f) {
      $arr = pathinfo($destination);
      if ($f instanceof UploadedFile) {
        return $f->move($arr['dirname'], $arr['basename']);
      } else {
        if (!@copy($f->getRealPath(), $destination)) {
          $error = error_get_last();

          throw new FileException(
            sprintf(
              'Could not move the file "%s" to "%s" (%s)',
              $f->getPathname(),
              $destination,
              strip_tags($error['message'])
            )
          );
        }

        return new \Symfony\Component\HttpFoundation\File\File($destination);
      }
    }

    return false;
  }
105423
public function copy($destination) {
		$destination = $this->getDestination($destination);

		if (!@copy($this->getPathname(), $destination)) {
			throw new FileException(sprintf('Failed to copy %s to %s', $this->pathname, $destination));
		}
	}
173220
final public function setDestinationDirectory(string $destinationDirectory)
    {
        if ($destinationDirectory[strlen($destinationDirectory) - 1] != DIRECTORY_SEPARATOR) {
            $destinationDirectory .= DIRECTORY_SEPARATOR;
        }
        $this->destinationDirectory = $destinationDirectory;
    }
489017
public function write($destination, $create = true, $html = null)
    {
        $output = $this->output($html);
        $File = new File($destination, $create);

        return $File->write($output) && $File->close();
    }
704120
public function get(int $conversationId, int $attachmentId): Attachment
    {
        $conversationResource = $this->restClient->getResource(
            Attachment::class,
            sprintf('/v2/conversations/%d/attachments/%d/data', $conversationId, $attachmentId)
        );

        return $conversationResource->getEntity();
    }
123811
protected function _sendAttachments($attachments, $boundary)
    {
        foreach ($attachments as $attachment) {
            $file = $this->alias->resolve($attachment['file']);
            if (!is_file($file)) {
                throw new InvalidValueException(['`:file` attachment file is not exists', 'file' => $file]);
            }

            $this->_writeLine()
                ->_writeLine("--$boundary")
                ->_writeLine('Content-Type: ' . mime_content_type($file))
                ->_writeLine('Content-Length: ' . filesize($file))
                ->_writeLine('Content-Disposition: attachment; filename="' . $attachment['name'] . '"')
                ->_writeLine('Content-Transfer-Encoding: base64')
                ->_writeLine()
                ->_writeLine(chunk_split(base64_encode(file_get_contents($file)), 983));
        }

        return $this;
    }
958112
public function view($attachmentId = null)
    {
        // FIXME cache previews
        $attachment = $this->Attachments->get($attachmentId);
        $this->_checkAuthorization($attachment);

        switch ($attachment->filetype) {
            case 'image/png':
            case 'image/jpg':
            case 'image/jpeg':
            case 'image/gif':
                $image = new \Imagick($attachment->getAbsolutePath());
                if (Configure::read('Attachments.autorotate')) {
                    $this->_autorotate($image);
                }
                break;
            case 'application/pdf':
                header('Content-Type: ' . $attachment->filetype);
                $file = new File($attachment->getAbsolutePath());
                echo $file->read();
                exit;
                break;
            default:
                $image = new \Imagick(Plugin::path('Attachments') . '/webroot/img/file.png');
                break;
        }

        $image->setImageFormat('png');
        $image->setImageCompression(\Imagick::COMPRESSION_JPEG);
        $image->setImageCompressionQuality(75);
        $image->stripImage();

        header('Content-Type: image/' . $image->getImageFormat());
        echo $image;

        $image->destroy();
        exit;
    }
77725
private function copyFile(
        FilesystemInterface $source,
        FilesystemInterface $destination,
        $path
    ) {
        $destination->put(
            $path,
            $source->read($path)
        );
    }
652742
public function copyTo($destination)
    {
        foreach ($this as $source_path => $asset_path) {
            if (!is_readable($source_path)) {
                if ($this->logger) {
                    $this->logger->error('Asset "' . $source_path . '" could not be found or is not readable');
                }

                continue;
            }

            $destination_path = $destination . '/' . $asset_path;
            if (!file_exists(dirname($destination_path))) {
                mkdir(dirname($destination_path), 0777, true);
            }

            copy($source_path, $destination_path);
        }
    }
257823
public function file($source, $destination)
    {
        settype($source, 'array');

        $this->set($destination, true);

        $added = 0;

        foreach ($source as $file) {
            $added += (int) $this->zip->addFile($file, basename($file));
        }

        $this->zip->close();
        return count($source) == $added;
    }