/**
  * Downloads a request
  *
  * @param RequestInterface $request
  *
  * @return boolean
  */
 public function download(RequestInterface $request, $path)
 {
     $response = $this->httpAdapter->sendRequest($request);
     if (!($body = $response->getBody())) {
         return false;
     }
     $stream = $body->detach();
     if (is_resource($stream)) {
         return $this->filesystem->putStream($path, $stream);
     }
     return $this->filesystem->put($path, $stream);
 }
예제 #2
0
파일: Backup.php 프로젝트: phalcon/forum
 public function generate()
 {
     if (PHP_SAPI != 'cli') {
         throw new \Exception("This script only can be used in CLI");
     }
     $config = $this->config->get('database');
     system(sprintf('/usr/bin/mysqldump -u %s -h %s -p%s -r /tmp/phosphorum.sql %s', $config->username, $config->host, $config->password, $config->dbname));
     system('bzip2 -f /tmp/phosphorum.sql');
     $config = $this->config->get('dropbox');
     if (!$config instanceof Config) {
         throw new \Exception("Unable to retrieve Dropbox credentials. Please check Forum Configuration");
     }
     if (!$config->get('appSecret') || !$config->get('accessToken')) {
         throw new \Exception("Please provide correct 'appSecret' and 'accessToken' config values");
     }
     $sourcePath = '/tmp/phosphorum.sql.bz2';
     if (!file_exists($sourcePath)) {
         throw new \Exception("Backup could not be created");
     }
     $client = new Client($config->get('accessToken'), $config->get('appSecret'));
     $adapter = new DropboxAdapter($client, $config->get('prefix', null));
     $filesystem = new Filesystem($adapter);
     $dropboxPath = '/phosphorum.sql.bz2';
     if ($filesystem->has($dropboxPath)) {
         $filesystem->delete($dropboxPath);
     }
     $fp = fopen($sourcePath, "rb");
     $filesystem->putStream($dropboxPath, $fp);
     fclose($fp);
     @unlink($sourcePath);
 }
예제 #3
0
 /**
  * Write the contents of a file with file path.
  *
  * @param string $path
  * @param string $filePath
  * @param array $config
  * @return mixed
  */
 public function putFile($path, $filePath, array $config = [])
 {
     $resource = fopen(Yii::getAlias($filePath), 'r');
     $result = parent::putStream($path, $resource, $config);
     fclose($resource);
     return $result;
 }
예제 #4
0
 public function testPushNotExists()
 {
     $file = '123-123-123';
     $path = sprintf('%s/%s.zip', $this->name, $file);
     $this->localFilesystem->readStream($path)->willReturn(false);
     $this->remoteFilesystem->putStream($path, Argument::any())->shouldNotBeCalled();
     $this->storage->push($file);
 }
 function it_handles_ivory_interface_incompatibility(Filesystem $filesystem, HttpAdapterInterface $httpAdapter, Request $request, Response $response, Stream $stream)
 {
     $httpAdapter->sendRequest($request)->willReturn($response);
     $response->getBody()->willReturn($stream);
     $stream->detach()->willReturn('text');
     $filesystem->putStream('path/to/file', Argument::type('resource'))->shouldNotBeCalled();
     $filesystem->put('path/to/file', 'text')->willReturn(true);
     $this->download($request, 'path/to/file')->shouldReturn(true);
 }
예제 #6
0
 public function testPutUpdateStream()
 {
     $path = 'path.txt';
     $stream = tmpfile();
     $this->prophecy->has($path)->willReturn(true);
     $this->prophecy->updateStream($path, $stream, $this->config)->willReturn(compact('path'));
     $this->assertTrue($this->filesystem->putStream($path, $stream));
     fclose($stream);
 }
예제 #7
0
 /**
  * @inheritdoc
  */
 public function putStream($path, $resource, array $config = [])
 {
     $addToIndex = !$this->has($path);
     $innerPath = $this->getInnerPath($path);
     $return = $this->fileSystem->putStream($innerPath, $resource, $config);
     if ($return !== false && $addToIndex) {
         $this->invokePlugin("addPathToIndex", [$path, $innerPath], $this);
     }
     return $return;
 }
예제 #8
0
 /**
  * {@inheritdoc}
  */
 public function push($file)
 {
     if (!$this->remoteFilesystem) {
         throw new RemoteStorageNotConfiguredException();
     }
     $path = sprintf('%s/%s.zip', $this->name, $file);
     if (false === ($stream = $this->localFilesystem->readStream($path)) || $this->remoteFilesystem->has($path)) {
         return;
     }
     $this->remoteFilesystem->putStream($path, $stream);
 }
 public function assembleChunks($chunks, $removeChunk, $renameChunk)
 {
     // the index is only added to be in sync with the filesystem storage
     $path = $this->prefix . '/' . $this->unhandledChunk['uuid'] . '/';
     $filename = $this->unhandledChunk['index'] . '_' . $this->unhandledChunk['original'];
     if (empty($chunks)) {
         $target = $filename;
     } else {
         sort($chunks, SORT_STRING | SORT_FLAG_CASE);
         $target = pathinfo($chunks[0], PATHINFO_BASENAME);
     }
     if ($this->unhandledChunk['index'] === 0) {
         // if it's the first chunk overwrite the already existing part
         // to avoid appending to earlier failed uploads
         $handle = fopen($path . '/' . $target, 'w');
     } else {
         $handle = fopen($path . '/' . $target, 'a');
     }
     $this->filesystem->putStream($path . $target, $handle);
     if ($renameChunk) {
         $name = preg_replace('/^(\\d+)_/', '', $target);
         /* The name can only match if the same user in the same session is
          * trying to upload a file under the same name AND the previous upload failed,
          * somewhere between this function, and the cleanup call. If that happened
          * the previous file is unaccessible by the user, but if it is not removed
          * it will block the user from trying to re-upload it.
          */
         if ($this->filesystem->has($path . $name)) {
             $this->filesystem->delete($path . $name);
         }
         $this->filesystem->rename($path . $target, $path . $name);
         $target = $name;
     }
     $uploaded = $this->filesystem->get($path . $target);
     if (!$renameChunk) {
         return $uploaded;
     }
     return new FlysystemFile($uploaded, $this->filesystem, $this->streamWrapperPrefix);
 }
 public function upload(FileInterface $file, $name, $path = null)
 {
     $path = is_null($path) ? $name : sprintf('%s/%s', $path, $name);
     if ($file instanceof FlysystemFile) {
         if ($file->getFilesystem() == $this->filesystem) {
             $file->getFilesystem()->rename($file->getPath(), $path);
             return new FlysystemFile($this->filesystem->get($path), $this->filesystem, $this->streamWrapperPrefix);
         }
     }
     $stream = fopen($file->getPathname(), 'r+');
     $this->filesystem->putStream($name, $stream);
     if (is_resource($stream)) {
         fclose($stream);
     }
     if ($file instanceof FlysystemFile) {
         $file->delete();
     } else {
         $filesystem = new LocalFilesystem();
         $filesystem->remove($file->getPathname());
     }
     return new FlysystemFile($this->filesystem->get($path), $this->filesystem, $this->streamWrapperPrefix);
 }
예제 #11
0
 /**
  * {@inheritdoc}
  */
 public function backup(Filesystem $source, Filesystem $destination, Database $database, array $parameter)
 {
     $tempFile = $this->temporaryFileSystem->createTemporaryFile('mysql');
     $process = new Process($this->getExportCommand($parameter['username'], $parameter['password'], $parameter['database'], $tempFile));
     $process->run();
     while ($process->isRunning()) {
         // waiting for process to finish
     }
     $handler = fopen($tempFile, 'r');
     $destination->putStream('dump.sql', $handler);
     fclose($handler);
     $this->output->writeln(sprintf('  * <comment>%s</comment>', $this->getExportCommand($parameter['username'], $parameter['password'], $parameter['database'], 'dump.sql', true)));
 }
예제 #12
0
 /**
  * @inheritdoc
  */
 protected function _save($fp, $dir, $name, $stat)
 {
     $path = $this->_joinPath($dir, $name);
     $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
     $config = [];
     if (isset(self::$mimetypes[$ext])) {
         $config['mimetype'] = self::$mimetypes[$ext];
     }
     if ($this->fs->putStream($path, $fp, $config)) {
         return $path;
     }
     return false;
 }
예제 #13
0
 /**
  * {@inheritdoc}
  */
 public function fopen($path, $mode)
 {
     $fullPath = $this->buildPath($path);
     $useExisting = true;
     switch ($mode) {
         case 'r':
         case 'rb':
             try {
                 return $this->flysystem->readStream($fullPath);
             } catch (FileNotFoundException $e) {
                 return false;
             }
         case 'w':
         case 'w+':
         case 'wb':
         case 'wb+':
             $useExisting = false;
         case 'a':
         case 'ab':
         case 'r+':
         case 'a+':
         case 'x':
         case 'x+':
         case 'c':
         case 'c+':
             //emulate these
             if ($useExisting and $this->file_exists($path)) {
                 if (!$this->isUpdatable($path)) {
                     return false;
                 }
                 $tmpFile = $this->getCachedFile($path);
             } else {
                 if (!$this->isCreatable(dirname($path))) {
                     return false;
                 }
                 $tmpFile = \OCP\Files::tmpFile();
             }
             $source = fopen($tmpFile, $mode);
             return CallbackWrapper::wrap($source, null, null, function () use($tmpFile, $fullPath) {
                 $this->flysystem->putStream($fullPath, fopen($tmpFile, 'r'));
                 unlink($tmpFile);
             });
     }
     return false;
 }
예제 #14
0
 /**
  * @dataProvider filesystemProvider
  */
 public function testPutStream(Filesystem $filesystem, $adapter, $cache)
 {
     $filesystem->flushCache();
     $stream = tmpfile();
     fwrite($stream, 'new content');
     $this->assertFalse($filesystem->has('new_file.txt'));
     $this->assertTrue($filesystem->putStream('new_file.txt', $stream));
     fclose($stream);
     unset($stream);
     $this->assertTrue($filesystem->has('new_file.txt'));
     $this->assertEquals('new content', $filesystem->read('new_file.txt'));
     $update = tmpfile();
     fwrite($update, 'modified content');
     $this->assertTrue($filesystem->putStream('new_file.txt', $update));
     $filesystem->flushCache();
     fclose($update);
     $this->assertEquals('modified content', $filesystem->read('new_file.txt'));
 }
예제 #15
0
function archive_backup($config, $files)
{
    info(__FUNCTION__ . ' start...<br />');
    $fs = new Filesystem(new ZipArchiveAdapter($config['archive']['target']));
    foreach ($files as $remote => $file) {
        // info(sprintf('put: %s to %s<br />', $file, $remote));
        try {
            $fp = fopen($file, 'r+');
            $fs->putStream($remote, $fp);
            fclose($fp);
        } catch (Exception $e) {
            info('<span class="color:red">error: ' . $e->getMessage() . '</span>');
            info('<span class="color:red">file: ' . $file . '</span>');
        }
    }
    $fs->getAdapter()->getArchive()->close();
    info(__FUNCTION__ . ' finished...<br />');
}
예제 #16
0
    /**
     * Creates an export of a instance
     *
     * @param Instance   $instance    The instance of the exports
     * @param array      $exports     Array of files to include in the snapshot
     * @param Filesystem $destination The destination upon which to place the export. Currently unused
     *                                If null, the instance's snapshot storage area is used.
     * @param int        $keepDays    The number of days to keep the snapshot
     *
     * @return array|boolean The snapshot metadata array or false on failure.
     */
    public function createFromExports(Instance $instance, array $exports, Filesystem $destination = null, $keepDays = EnterpriseDefaults::SNAPSHOT_DAYS_TO_KEEP)
    {
        //  Build our "mise en place", as it were...
        $_success = false;
        $_stamp = date('YmdHis');
        //  Create the snapshot ID
        $_snapshotId = $_stamp . '.' . Inflector::neutralize($instance->instance_name_text);
        $_snapshotName = str_replace('{id}', $_snapshotId, config('snapshot.templates.snapshot-file-name'));
        //  Set up the temp dir
        $this->setWorkPath($instance->getSnapshotPath());
        //  Create the snapshot archive and stuff it full of goodies
        /** @var SnapshotManifest $_manifest */
        list($_fsSnapshot, $_manifest, $_routeHash, $_routeLink) = $this->createExportArchive($_snapshotId, $_snapshotName, ['timestamp' => $_stamp, 'guest-location' => $instance->guest_location_nbr, 'instance-id' => $instance->instance_id_text, 'cluster-id' => (int) $instance->cluster_id, 'db-server-id' => (int) $instance->db_server_id, 'web-server-id' => (int) $instance->web_server_id, 'app-server-id' => (int) $instance->app_server_id, 'owner-id' => (int) $instance->user->id, 'owner-email-address' => $instance->user->email_addr_text, 'owner-storage-key' => $instance->user->storage_id_text, 'storage-key' => $instance->storage_id_text], $keepDays);
        try {
            $this->addFilesToArchive($exports, $_fsSnapshot);
            try {
                //  Write our snapshot manifesto
                $_manifest->write();
                //  Close up the files
                /** @noinspection PhpUndefinedMethodInspection */
                $this->flushZipArchive($_fsSnapshot);
                //  Look up the hash entry
                if (null === ($_routeHash = RouteHash::byHash($_routeHash)->first())) {
                    throw new \LogicException('Previously created route hash not found.');
                }
                //  Create our snapshot record
                Snapshot::create(['user_id' => $instance->user_id, 'instance_id' => $instance->id, 'route_hash_id' => $_routeHash->id, 'snapshot_id_text' => $_snapshotId, 'public_ind' => true, 'public_url_text' => $_routeLink, 'expire_date' => $_routeHash->expire_date]);
                //  Copy to $destination if requested
                if ($destination) {
                    if (false === ($_fd = fopen($this->workPath . $_snapshotName, 'r'))) {
                        throw new FileSystemException('Unable to open export file "' . $this->workPath . $_snapshotName . '".');
                    }
                    $destination->putStream($_snapshotName, $_fd);
                    fclose($_fd);
                }
                //  Let the user know...
                $this->notifyInstanceOwner($instance, 'Export successful', ['firstName' => $instance->user->first_name_text, 'headTitle' => 'Export Complete', 'contentHeader' => 'Your export has completed', 'emailBody' => <<<HTML
<p>Your export is complete. It may be downloaded it for up to {$keepDays} days, from the following link:<br/>
<br/>
<strong><a href="{$_routeLink}" target="_blank">{$_routeLink}</a></strong>
</p>
HTML
]);
                $_success = true;
            } catch (\Exception $_ex) {
                $this->error('exception building snapshot archive: ' . $_ex->getMessage());
                throw $_ex;
            }
        } catch (\Exception $_ex) {
            $this->error('exception during sub-provisioner export call: ' . $_ex->getMessage());
            $this->notifyInstanceOwner($instance, 'Export failure', ['firstName' => $instance->user->first_name_text, 'headTitle' => 'Export Failure', 'contentHeader' => 'Your export was not created', 'emailBody' => <<<HTML
<p>The export requested did not complete properly. Please make sure your instance is up and running, then try again. If the issue persists, please contact support.</p>
HTML
]);
            $_success = false;
        } finally {
            //  Cleanup
            $_fsSnapshot = null;
        }
        return $_success ? $_manifest->toArray() : false;
    }
 /**
  * Move a file (and its associative variants) between filesystems
  *
  * @param string $fileID
  * @param Filesystem $from
  * @param Filesystem $to
  */
 protected function moveBetweenFilesystems($fileID, Filesystem $from, Filesystem $to)
 {
     foreach ($this->findVariants($fileID, $from) as $nextID) {
         // Copy via stream
         $stream = $from->readStream($nextID);
         $to->putStream($nextID, $stream);
         fclose($stream);
         $from->delete($nextID);
     }
     // Truncate empty dirs
     $this->truncateDirectory(dirname($fileID), $from);
 }
예제 #18
0
 /**
  * @param string $path     path to file
  * @param string $contents file contents
  * @param mixed  $config
  * @throws FileExistsException
  * @return bool
  */
 public function putStream($path, $resource, array $config = [])
 {
     $result = parent::putStream($path, $resource, $config);
     if ($result && ($resource = $this->get($path))) {
         return $this->dispatch(new SyncFile($resource));
     }
     return $result;
 }