/**
  * Delete the temporary file.
  *
  * @param Model $model
  * @return bool
  */
 public function afterValidate(Model $model)
 {
     if ($this->_tempFile) {
         $this->_tempFile->delete();
     }
     return true;
 }
 /**
  * Attempt to delete a file using the attachment settings.
  *
  * @uses Transit\File
  *
  * @param Model $model
  * @param string $field
  * @param string $path
  * @return bool
  */
 protected function _deleteFile(Model $model, $field, $path)
 {
     if (empty($this->settings[$model->alias][$field])) {
         return false;
     }
     $attachment = $this->_settingsCallback($model, $this->settings[$model->alias][$field]);
     $basePath = $attachment['uploadDir'] ?: $attachment['tempDir'];
     try {
         // Delete remote file
         if ($attachment['transport']) {
             $transporter = $this->_getTransporter($attachment['transport']);
             return $transporter->delete($path);
             // Delete local file
         } else {
             $file = new File($basePath . basename($path));
             return $file->delete();
         }
     } catch (Exception $e) {
         $this->log($e->getMessage(), LOG_DEBUG);
     }
     return false;
 }
 /**
  * Transport the file to Amazon Glacier and return the archive ID.
  *
  * @param \Transit\File $file
  * @return string
  * @throws \Transit\Exception\TransportationException
  */
 public function transport(File $file)
 {
     $config = $this->_config;
     $response = null;
     // If larger then 100MB, split upload into parts
     if ($file->size() >= 100 * Size::MB) {
         $uploader = UploadBuilder::newInstance()->setClient($this->getClient())->setSource($file->path())->setVaultName($config['vault'])->setAccountId($config['accountId'] ?: '-')->setPartSize(10 * Size::MB)->build();
         try {
             $response = $uploader->upload();
         } catch (MultipartUploadException $e) {
             $uploader->abort();
         }
     } else {
         $response = $this->getClient()->uploadArchive(array_filter(array('vaultName' => $config['vault'], 'accountId' => $config['accountId'], 'body' => EntityBody::factory(fopen($file->path(), 'r')))));
     }
     // Return archive ID if successful
     if ($response) {
         $file->delete();
         return $response->getPath('archiveId');
     }
     throw new TransportationException(sprintf('Failed to transport %s to Amazon Glacier', $file->basename()));
 }
 /**
  * Transport the file to a remote location.
  *
  * @param \Transit\File $file
  * @return string
  * @throws \Transit\Exception\TransportationException
  */
 public function transport(File $file)
 {
     $config = $this->_config;
     $key = ltrim($config['folder'], '/') . $file->basename();
     $response = null;
     // If larger then 100MB, split upload into parts
     if ($file->size() >= 100 * Size::MB) {
         $uploader = UploadBuilder::newInstance()->setClient($this->getClient())->setSource($file->path())->setBucket($config['bucket'])->setKey($key)->setMinPartSize(10 * Size::MB)->build();
         try {
             $response = $uploader->upload();
         } catch (MultipartUploadException $e) {
             $uploader->abort();
         }
     } else {
         $response = $this->getClient()->putObject(array_filter(array('Key' => $key, 'Bucket' => $config['bucket'], 'Body' => EntityBody::factory(fopen($file->path(), 'r')), 'ACL' => $config['acl'], 'ContentType' => $file->type(), 'ServerSideEncryption' => $config['encryption'], 'StorageClass' => $config['storage'], 'Metadata' => $config['meta'])));
     }
     // Return S3 URL if successful
     if ($response) {
         $file->delete();
         return sprintf('%s/%s/%s', S3Client::getEndpoint($this->getClient()->getDescription(), $config['region'], $config['scheme']), $config['bucket'], $key);
     }
     throw new TransportationException(sprintf('Failed to transport %s to Amazon S3', $file->basename()));
 }
Example #5
0
 /**
  * Test that move() doesn't overwrite files but appends an incremented number.
  */
 public function testMoveNoOverwrite()
 {
     $testPath = TEST_DIR . '/test.jpg';
     $movePath = TEMP_DIR . '/test-1.jpg';
     copy($this->baseFile, $testPath);
     $this->assertFalse(file_exists($movePath));
     $file = new File($testPath);
     $file->move(TEMP_DIR);
     $this->assertTrue(file_exists($movePath));
     $file->delete();
 }