Skip to content

Commit

Permalink
Add get.php support
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwindell committed Dec 7, 2021
1 parent 9326a9b commit e0e0fd2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Model/File/Storage/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public function loadByFilename(string $filename)
/** @var string $relativePath */
$relativePath = $this->storageHelper->getMediaRelativePath($filename);

// @todo look up media path like https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/MediaStorage/App/Media.php#L187
$relativePath = str_replace('media/', '', $relativePath);

if ($this->getStorage()->objectExists($relativePath)) {
$this->setData('id', $filename);
$this->setData('filename', $filename);
Expand Down
52 changes: 52 additions & 0 deletions Model/File/Storage/Synchronization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace AuroraExtensions\GoogleCloudStorage\Model\File\Storage;

use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Directory\WriteInterface as DirectoryWrite;

class Synchronization
{
/**
* @var BucketFactory
*/
protected $bucket;

/**
* File stream handler
*
* @var DirectoryWrite
*/
protected $mediaDirectory;

public function __construct(
DirectoryWrite $directory,
BucketFactory $bucket
) {
$this->mediaDirectory = $directory;
$this->bucket = $bucket;
}

/**
* Synchronize file from GCS to local filesystem
*
* @param string $relativeFileName
* @return void
*/
public function synchronize($relativeFileName)
{
$storage = $this->bucket->loadByFilename($relativeFileName);

if ($storage->getId()) {
$file = $this->mediaDirectory->openFile($relativeFileName, 'w');
try {
$file->lock();
$file->write($storage->getContent());
$file->unlock();
$file->close();
} catch (FileSystemException $e) {
$file->close();
}
}
}
}
3 changes: 3 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<preference for="AuroraExtensions\GoogleCloudStorage\Api\StorageObjectManagementInterface"
type="AuroraExtensions\GoogleCloudStorage\Model\Adapter\StorageObjectManagement"/>

<preference for="Magento\MediaStorage\Model\File\Storage\Synchronization"
type="AuroraExtensions\GoogleCloudStorage\Model\File\Storage\Synchronization"/>

<virtualType name="AuroraExtensions\GoogleCloudStorage\Model\System\Source\Select\Bucket\Acl"
type="AuroraExtensions\ModuleComponents\Model\Config\Source\Select\VirtualSelect">
<arguments>
Expand Down

0 comments on commit e0e0fd2

Please sign in to comment.