Alibaba Cloud OSS Stream Wrapper Implement

Submitted by august on Thu, 04/19/2018 - 23:06

A piece of code about implement Alibaba cloud OSS stream wrapper interface. (base Drupal 8 and flysystem)

<?php

namespace Drupal\flysystem_oss\StreamWrapper;

use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\Site\Settings;
use OSS\OssClient;
use OSS\Core\OssException;
use Drupal\flysystem\Plugin\ImageStyleGenerationTrait;
use Drupal\Component\Utility\UrlHelper;


/**
 * Defines a Drupal stream wrapper base class for alicloud OSS files.
 *
 * This class provides a complete stream wrapper implementation. URIs such as
 * "public://example.txt" are expanded to a normal filesystem path such as
 * "sites/default/files/example.txt" and then PHP filesystem functions are
 * invoked.
 *
 * Drupal\Core\StreamWrapper\LocalStream implementations need to implement at least the
 * getDirectoryPath() and getExternalUrl() methods.
 */
abstract class OssStreamWrapper implements StreamWrapperInterface {
  use ImageStyleGenerationTrait;

 ...

  /**
   * Provides information for is_dir, is_file, filesize, etc. Works on
   * buckets, keys, and prefixes.
   * @link http://www.php.net/manual/en/streamwrapper.url-stat.php
   */
  public function url_stat($path, $flags) {
        $stat = [
          'dev' => 0,
          'ino' => 0,
          'mode' => 0777,
          'nlink' => 0,
          'uid' => 0,
          'gid' => 0,
          'rdev' => 0,
          'size' => 0,
          'atime' => 0,
          'mtime' => 0,
          'ctime' => 0,
          'blksize' => 0,
          'blocks' => 0,
        ];

        $name = $this->getObjectName($path);
        // Check if this path is in the url_stat cache
        if ($stat_cache = $this->getCacheStorage()->get($name)) {
            return $stat_cache;
        }
        try {
            $info = $this->client->getObjectMeta($this->bucket, $name);
            if (isset($info['info']) && !empty($info['info'])) {
                $info = $info['info'];
                $stat['size'] = $info['download_content_length'];
                $stat['atime'] = time();
                $stat['mtime'] = $info['filetime'];
                $stat['mode'] |= 0100000;
            }
        } catch (OssException $e) {
            // $stat['mode'] |= 040000;
            // Maybe this isn't an actual key, but a prefix. Do a prefix
            // listing of objects to determine.
            $result = $this->client->listObjects($this->bucket, [
              'prefix' => $name,
              'max-keys' => 1,
            ]);
            $prefixList = $result->getPrefixList();

            if (!empty($prefixList)) {
                $stat['mode'] |= 040000;
            } else {
                $stat = FALSE;
            }

        }
        $this->getCacheStorage()->set($name, $stat);

        return $stat;
  }

 ...
}

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.