thephpleague / flysystem-rackspace

Flysystem Adapter for Rackspace
37 stars 28 forks source link

Creating directories #14

Closed rvmourik closed 8 years ago

rvmourik commented 8 years ago

Hi,

We're using the adapter to use an object store as our filesystem, but creating directories was not working. After investigating we got to the conclusion the createDir method only returns a array with the dirname.

Altough Rackspace doesn't officially supports the use of folders, there is a way of getting this to work. In order for directories to work you need to send two additional headers with the request.

Contents: None
Content-Type: application/directory

I've changed the createDir and normalizeObject to work with this solution.

createDir

public function createDir($dirname, Config $config)
    {
//        return ['path' => $dirname];
        $headers = [];

        if ($config->has('headers')) {
            $headers = $config->get('headers');
        }

        if (!key_exists('Contents', $headers)) {
            $headers['Contents'] = 'None';
        }

        if (!key_exists('Content-Type', $headers)) {
            $headers['Content-Type'] = 'application/directory';
        }

        $config->set('headers', $headers);

        return $this->write($dirname, '', $config);
    }

normalizeObject

protected function normalizeObject(DataObject $object)
    {
        $name = $object->getName();
        $name = $this->removePathPrefix($name);
        $mimetype = explode('; ', $object->getContentType());

        return [
            'type'      => ((in_array('application/directory', $mimetype)) ? 'dir' : 'file'),
            'dirname'   => Util::dirname($name),
            'path'      => $name,
            'timestamp' => strtotime($object->getLastModified()),
            'mimetype'  => reset($mimetype),
            'size'      => $object->getContentLength(),
        ];
    }

Is this something you'll be willing to integrate in the adapter, if so do you need a pull request (never done that before :-)) or can you manually use the above code adjustments.

rvmourik commented 8 years ago

Hi @frankdejonge ,

Is it possible to check the above issue and tell me if this is something that's gonna be integrated?

Thanks in advance.

Robbert

frankdejonge commented 8 years ago

@rvmourik I'm not sure about this one. With AWS there's a "supported" way, with rackspace it's not. Which would introduce a "flysystem flavoured" way of interacting with rackspace, making it less transparent. Is there a need for creating the directory? Are you creating a file manager of some sorts?

rvmourik commented 8 years ago

Yes, it's going to be connected to a filemanager (elfinder for now).

But if you can take a look here https://developer.rackspace.com/docs/cloud-files/v1/developer-guide/ and you search for "application/directory", you'll get to the informatie how directories are "supported".

Isn't that their way of doing it and not a "flysystem flavoured" way like you suggested?

frankdejonge commented 8 years ago

@rvmourik ah I see. A PR is welcome then :+1:

rvmourik commented 8 years ago

@frankdejonge Going to try that, never done that before :-)