public function __construct($path) { $this->_path = $path; if (!is_readable($path)) { require_once 'Sitengine/Amazon/S3/Exception.php'; throw new Sitengine_Amazon_S3_Exception('invaid file specified'); } require_once 'Sitengine/Mime/Type.php'; $this->_mime = Sitengine_Mime_Type::get(preg_replace('/.*\\.(\\w*)$/', '$1', $path)); $this->_size = filesize($path); $this->_md5 = md5_file($path); }
protected function _saveImportedFile($fileId, $sourcePath, $filename) { try { $width = 0; $height = 0; $mime = Sitengine_Mime_Type::get($sourcePath); if (!is_writeable($sourcePath) || !is_file($sourcePath)) { throw new Sitengine_Exception('file can not be accessed'); } if (Sitengine_Mime_Type::isImage($mime)) { $info = getimagesize($sourcePath); if (!$info) { $this->_rollbackFileImport(); throw new Sitengine_Exception('file is not an image'); } $width = $info[0]; $height = $info[1]; } $data = array('name' => $filename, 'source' => basename($sourcePath), 'mime' => $mime, 'size' => filesize($sourcePath), 'width' => $width, 'height' => $height); $finalPath = $this->_configs[$fileId]['dir'] . '/' . $filename; if (!copy($sourcePath, $finalPath)) { throw new Sitengine_Exception('file could not be copied'); } if (!unlink($sourcePath)) { throw new Sitengine_Exception('source file could not be deleted'); } $this->_importedFiles[$fileId] = array('sourcePath' => $sourcePath, 'finalPath' => $finalPath); $this->_files[$fileId] = $data; } catch (Exception $exception) { $this->_rollbackFileImport(); throw new Sitengine_Exception('file import failed', $exception); } }
public function handleInsertUploads($id) { try { $upload = new Sitengine_Upload(self::FILE1ORIGINAL_ID); if (!$upload->isFile()) { # upload comes from spinelab flash applet $upload = new Sitengine_Upload('Filedata'); } if ($upload->isFile()) { $suffix = Sitengine_Mime_Type::getSuffix($upload->getMime()); if (!$suffix) { # fix suffix if file is being uploaded through spinelab flash app $suffix = '.' . preg_replace('/.*\\.(\\w+)$/', "\$1", $upload->getName()); } $file1OriginalName = $this->makeFileName(self::FILE1ORIGINAL_ID, $id, $suffix); #$key = $this->_configs[self::FILE1ORIGINAL_ID]['prefix'].'/'.$file1OriginalName; require_once 'Sitengine/Amazon/S3/Object.php'; $object = new Sitengine_Amazon_S3_Object($this->_configs[self::FILE1ORIGINAL_ID]['connection'], $this->_configs[self::FILE1ORIGINAL_ID]['bucket'], $this->_configs[self::FILE1ORIGINAL_ID]['prefix'] . '/' . $file1OriginalName, $this->_configs[self::FILE1ORIGINAL_ID]['cname'], $this->_configs[self::FILE1ORIGINAL_ID]['ssl']); #$object = $this->_configs[self::FILE1ORIGINAL_ID]['object']; $response = $object->head(); if ($response->getHttpResponse()->getStatus() == 200) { require_once 'Sitengine/Blog/Exception.php'; throw new Sitengine_Blog_Exception('insert upload error on file1 (duplicate id)'); } $mime = Sitengine_Mime_Type::get($suffix); if (Sitengine_Mime_Type::isJpg($mime) || Sitengine_Mime_Type::isGif($mime) || Sitengine_Mime_Type::isPng($mime)) { $this->_resizeSaveUploadedImage(self::FILE1THUMBNAIL_ID, $upload, $id); } $this->_saveUploadedFile(self::FILE1ORIGINAL_ID, $upload, $file1OriginalName); } } catch (Exception $exception) { $this->_rollback(); require_once 'Sitengine/Blog/Exception.php'; throw new Sitengine_Blog_Exception('handle insert upload error', $exception); } }
protected function _copyKeys(array $keys, $targetPrefix, array $headers = array(), array $amzHeaders = array()) { foreach ($keys as $key) { $currAmzHeaders = $amzHeaders; $this->_countKeys++; require_once 'Sitengine/Mime/Type.php'; $mime = Sitengine_Mime_Type::get($key); $targetKey = $targetPrefix . $key; if ($this->_targetRenamePatternFind !== null && $this->_targetRenamePatternReplace !== null) { $targetKey = preg_replace($this->_targetRenamePatternFind, $this->_targetRenamePatternReplace, $targetKey); } try { $this->_print('copying: ' . $key . ' -> ' . $targetKey); $exclude = false; foreach ($this->_excludePatterns as $pattern) { if (preg_match($pattern, $key)) { $exclude = true; } } if ($exclude) { $this->_print(" -> EXCLUDED\n"); $this->_countExcluded++; continue; } require_once 'Sitengine/Amazon/S3/Object.php'; $targetObject = new Sitengine_Amazon_S3_Object($this->_connection, $this->_targetBucketName, $targetKey); $head = null; if (!$this->_force) { # check if target exists $head = $targetObject->head(); if ($head->getHttpResponse()->getStatus() == 200) { $eTag = $head->getClient()->getLastResponse()->getHeader('Etag'); if ($eTag !== null) { # set pre-condition $currAmzHeaders[] = "x-amz-copy-source-if-none-match: {$eTag}"; } /* $this->_print(" -> TARGET EXISTS (SKIPPED)\n"); $this->_countSkipped++; continue; */ } } $response = $targetObject->copy($this->_sourceBucket->getName(), $key, $mime, $headers, $currAmzHeaders); $code = $response->getHttpResponse()->extractCode($response->getHttpResponse()->getHeadersAsString()); if ($code == 412) { $this->_print(" -> SKIPPED (EXISTS)\n"); $this->_countSkipped++; continue; } if ($response->isError()) { # try again $response = $targetObject->copy($this->_sourceBucket->getName(), $key, $mime, $headers, $currAmzHeaders); if ($response->isError()) { #print $response->getErrorMessage()."\n"; require_once 'Sitengine/Amazon/S3/Utils/Exception.php'; throw new Sitengine_Amazon_S3_Utils_Exception('Copy Error'); } } if ($head !== null && $head->getHttpResponse()->getStatus() == 200) { $this->_countReplaced++; $this->_print(" -> REPLACE OK\n"); } else { $this->_countCopied++; $this->_print(" -> COPY OK\n"); } #$this->_countCopied++; #$this->_print(" -> OK\n"); #$response = $targetObject->head($key); #print $response->getHttpResponse()->asString(); } catch (Exception $exception) { $this->_countErrors++; $msg = " -> ERROR (" . $exception->getMessage() . ")\n"; $this->_print($msg); } } }
protected function _rollback() { #Sitengine_Debug::print_r($this->_newFiles); #Sitengine_Debug::print_r($this->_currentFiles); # remove new files foreach ($this->_newFiles as $fileId => $name) { #$key = $this->_configs[$fileId]['prefix'].'/'.$name; #$object = $this->_configs[$fileId]['object']; require_once 'Sitengine/Amazon/S3/Object.php'; $object = new Sitengine_Amazon_S3_Object($this->_configs[$fileId]['connection'], $this->_configs[$fileId]['bucket'], $this->_configs[$fileId]['prefix'] . '/' . $name, $this->_configs[$fileId]['cname'], $this->_configs[$fileId]['ssl']); $response = $object->delete(); } # restore current files foreach ($this->_currentFiles as $fileId => $name) { #$key = $this->_configs[$fileId]['prefix'].'/'.$name; $rollbackKey = 'Temp/' . $this->_configs[$fileId]['prefix'] . '/' . $name; #$object = $this->_configs[$fileId]['object']; require_once 'Sitengine/Amazon/S3/Object.php'; $object = new Sitengine_Amazon_S3_Object($this->_configs[$fileId]['connection'], $this->_configs[$fileId]['bucket'], $this->_configs[$fileId]['prefix'] . '/' . $name, $this->_configs[$fileId]['cname'], $this->_configs[$fileId]['ssl']); require_once 'Sitengine/Amazon/S3/Object.php'; $rollbackObject = new Sitengine_Amazon_S3_Object($this->_configs[$fileId]['connection'], $this->_configs[$fileId]['bucket'], $rollbackKey, $this->_configs[$fileId]['cname'], $this->_configs[$fileId]['ssl']); require_once 'Sitengine/Mime/Type.php'; $mime = Sitengine_Mime_Type::get($name); $response = $object->copy($rollbackObject->getBucketName(), $rollbackKey, $mime, array(), $this->_configs[$fileId]['amzHeaders']); $response = $rollbackObject->delete(); } $this->_newFiles = array(); $this->_currentFiles = array(); }