| 1 |
<?php |
| 2 |
namespace WPIDE\App\Helpers; |
| 3 |
|
| 4 |
use Exception; |
| 5 |
use WPIDE\App\Services\Storage\Filesystem; |
| 6 |
use WPIDE\App\Services\Storage\LocalFileSystem; |
| 7 |
|
| 8 |
use const WPIDE\Constants\IMAGE_DATA_DIR; |
| 9 |
|
| 10 |
class ImageStateData |
| 11 |
{ |
| 12 |
|
| 13 |
protected static $storage = null; |
| 14 |
|
| 15 |
public static function init() { |
| 16 |
|
| 17 |
EmptyDir::create(IMAGE_DATA_DIR); |
| 18 |
} |
| 19 |
|
| 20 |
public static function getStateFile($id): string |
| 21 |
{ |
| 22 |
return $id.'.json'; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @throws Exception |
| 27 |
*/ |
| 28 |
public static function save($item, $state): bool |
| 29 |
{ |
| 30 |
|
| 31 |
self::createStateFile($item->id, $state); |
| 32 |
|
| 33 |
$dataFile = $item->id.'.'.$item->ext; |
| 34 |
|
| 35 |
if(!self::imageFileExists($dataFile)) { |
| 36 |
|
| 37 |
$rootStorage = LocalFileSystem::load(); |
| 38 |
$stream = $rootStorage->readStream($item->path); |
| 39 |
if (!self::storage()->storeStream('/', $dataFile, $stream['stream'], true)) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
public static function createStateFile($id, $state) { |
| 48 |
|
| 49 |
$stateFile = self::getStateFile($id); |
| 50 |
|
| 51 |
if(!self::storage()->storeStreamFromContent('/', $stateFile, $state, true)) { |
| 52 |
throw new Exception('Cannot create image state file!'); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public static function stateFileExists($id): bool |
| 57 |
{ |
| 58 |
$stateFile = self::getStateFile($id); |
| 59 |
return self::storage()->fileExists($stateFile); |
| 60 |
} |
| 61 |
|
| 62 |
public static function imageFileExists($name): bool |
| 63 |
{ |
| 64 |
return self::storage()->fileExists($name); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @throws Exception |
| 69 |
*/ |
| 70 |
public static function getState($id): ?array |
| 71 |
{ |
| 72 |
|
| 73 |
if(!self::stateFileExists($id)) { |
| 74 |
throw new Exception('Image state file not found'); |
| 75 |
} |
| 76 |
|
| 77 |
$stateFile = self::getStateFile($id); |
| 78 |
|
| 79 |
return self::storage()->read($stateFile); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @throws Exception |
| 84 |
*/ |
| 85 |
public static function deleteState($id): bool |
| 86 |
{ |
| 87 |
|
| 88 |
if(!self::stateFileExists($id)) { |
| 89 |
throw new Exception('Image state file not found'); |
| 90 |
} |
| 91 |
|
| 92 |
$stateFile = self::getStateFile($id); |
| 93 |
|
| 94 |
return self::storage()->deleteFile($stateFile); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @throws Exception |
| 99 |
*/ |
| 100 |
public static function getImage($name): ?array |
| 101 |
{ |
| 102 |
return self::storage()->readStream($name); |
| 103 |
} |
| 104 |
|
| 105 |
protected function getEmptyState(): string |
| 106 |
{ |
| 107 |
|
| 108 |
return '{ |
| 109 |
"canvas": { |
| 110 |
"version": "4.6.0", |
| 111 |
"objects": [] |
| 112 |
}, |
| 113 |
"editor": { |
| 114 |
"frame": null, |
| 115 |
"zoom": 1, |
| 116 |
"activeObjectId": null |
| 117 |
}, |
| 118 |
"canvasWidth": 3000, |
| 119 |
"canvasHeight": 2500 |
| 120 |
}'; |
| 121 |
} |
| 122 |
|
| 123 |
protected static function storage(): Filesystem |
| 124 |
{ |
| 125 |
if(!self::$storage) { |
| 126 |
self::$storage = LocalFileSystem::load(IMAGE_DATA_DIR); |
| 127 |
} |
| 128 |
|
| 129 |
return self::$storage; |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|