/** * @param \CF_Container $container * @param \CF_Object $object */ function it_should_lazily_fetch_container_before_write($connection, $container, $object) { $connection->get_container('my_container')->willReturn($container)->shouldBeCalled(); $object->write('some content')->shouldBeCalled()->willReturn(true); $container->get_object('filename')->shouldBeCalled()->willReturn($object); $this->write('filename', 'some content')->shouldReturn(12); }
/** * Helper function to create "path" elements for a given Object name * * Given an Object whos name contains '/' path separators, this function * will create the "directory marker" Objects of one byte with the * Content-Type of "application/folder". * * It assumes the last element of the full path is the "real" Object * and does NOT create a remote storage Object for that last element. */ function create_paths($path_name) { if ($path_name[0] == '/') { $path_name = mb_substr($path_name, 0, 1); } $elements = explode('/', $path_name, -1); $build_path = ""; foreach ($elements as $idx => $val) { if (!$build_path) { $build_path = $val; } else { $build_path .= "/" . $val; } $obj = new CF_Object($this, $build_path); $obj->content_type = "application/directory"; $obj->write(".", 1); } }
/** * @see FileBackendStore::doCreateInternal() * @return Status */ protected function doCreateInternal(array $params) { $status = Status::newGood(); list($dstCont, $dstRel) = $this->resolveStoragePathReal($params['dst']); if ($dstRel === null) { $status->fatal('backend-fail-invalidpath', $params['dst']); return $status; } // (a) Check the destination container and object try { $dContObj = $this->getContainer($dstCont); } catch (NoSuchContainerException $e) { $status->fatal('backend-fail-create', $params['dst']); return $status; } catch (CloudFilesException $e) { // some other exception? $this->handleException($e, $status, __METHOD__, $params); return $status; } // (b) Get a SHA-1 hash of the object $sha1Hash = wfBaseConvert(sha1($params['content']), 16, 36, 31); // (c) Actually create the object try { // Create a fresh CF_Object with no fields preloaded. // We don't want to preserve headers, metadata, and such. $obj = new CF_Object($dContObj, $dstRel, false, false); // skip HEAD $obj->setMetadataValues(array('Sha1base36' => $sha1Hash)); // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59). // The MD5 here will be checked within Swift against its own MD5. $obj->set_etag(md5($params['content'])); // Use the same content type as StreamFile for security $obj->content_type = StreamFile::contentTypeFromPath($params['dst']); if (!strlen($obj->content_type)) { // special case $obj->content_type = 'unknown/unknown'; } // Set any other custom headers if requested if (isset($params['headers'])) { $obj->headers += $this->sanitizeHdrs($params['headers']); } if (!empty($params['async'])) { // deferred $op = $obj->write_async($params['content']); $status->value = new SwiftFileOpHandle($this, $params, 'Create', $op); $status->value->affectedObjects[] = $obj; } else { // actually write the object in Swift $obj->write($params['content']); $this->purgeCDNCache(array($obj)); } } catch (CDNNotEnabledException $e) { // CDN not enabled; nothing to see here } catch (BadContentTypeException $e) { $status->fatal('backend-fail-contenttype', $params['dst']); } catch (CloudFilesException $e) { // some other exception? $this->handleException($e, $status, __METHOD__, $params); } return $status; }
/** * @see FileBackendStore::doCreateInternal() */ protected function doCreateInternal(array $params) { $status = Status::newGood(); list($dstCont, $dstRel) = $this->resolveStoragePathReal($params['dst']); if ($dstRel === null) { $status->fatal('backend-fail-invalidpath', $params['dst']); return $status; } // (a) Check the destination container and object try { unset($this->objCache[$params['dst']]); $dContObj = $this->getContainer($dstCont); if (empty($params['overwrite']) && $this->fileExists(array('src' => $params['dst'], 'latest' => 1))) { $status->fatal('backend-fail-alreadyexists', $params['dst']); return $status; } } catch (NoSuchContainerException $e) { $status->fatal('backend-fail-create', $params['dst']); $this->logException($e, __METHOD__, $params); return $status; } catch (InvalidResponseException $e) { $status->fatal('backend-fail-connect', $this->name); $this->logException($e, __METHOD__, $params); return $status; } catch (Exception $e) { // some other exception? $status->fatal('backend-fail-internal', $this->name); $this->logException($e, __METHOD__, $params); return $status; } // (b) Get a SHA-1 hash of the object $sha1Hash = wfBaseConvert(sha1($params['content']), 16, 36, 31); // (c) Actually create the object try { // Create a fresh CF_Object with no fields preloaded. // We don't want to preserve headers, metadata, and such. $obj = new CF_Object($dContObj, $dstRel, false, false); // skip HEAD // Note: metadata keys stored as [Upper case char][[Lower case char]...] $obj->metadata = array('Sha1base36' => $sha1Hash); // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59). // The MD5 here will be checked within Swift against its own MD5. $obj->set_etag(md5($params['content'])); // Use the same content type as StreamFile for security $obj->content_type = StreamFile::contentTypeFromPath($params['dst']); // Actually write the object in Swift $obj->write($params['content']); } catch (BadContentTypeException $e) { $status->fatal('backend-fail-contenttype', $params['dst']); $this->logException($e, __METHOD__, $params); } catch (InvalidResponseException $e) { $status->fatal('backend-fail-connect', $this->name); $this->logException($e, __METHOD__, $params); } catch (Exception $e) { // some other exception? $status->fatal('backend-fail-internal', $this->name); $this->logException($e, __METHOD__, $params); } return $status; }
/** * @param \CF_Container $container * @param \CF_Object $fromObject * @param \CF_Object $toObject */ function it_should_rename_file($container, $fromObject, $toObject) { $fromObject->read()->willReturn('some content'); $toObject->write('some content')->willReturn(true); $container->get_object('filename')->willReturn($fromObject); $container->get_object('filename1')->willReturn($toObject); $this->rename('filename', 'filename1')->shouldReturn(true); }
/** * * Make a dir * package by lazypeople<*****@*****.**> */ function sae_mkdir($path_name) { if ($path_name[0] == '/') { $path_name = mb_substr($path_name, 0, 1); } $dir_name = $path_name . '/' . $path_name; $obj = new CF_Object($this, $dir_name); $obj->content_type = "text/plain"; $obj->write("___sae__dir__tag__"); return true; }