|
32 | 32 | use craft\events\GetAssetThumbUrlEvent; |
33 | 33 | use craft\events\GetAssetUrlEvent; |
34 | 34 | use craft\events\ReplaceAssetEvent; |
| 35 | +use craft\helpers\ArrayHelper; |
35 | 36 | use craft\helpers\Assets as AssetsHelper; |
36 | 37 | use craft\helpers\DateTimeHelper; |
37 | 38 | use craft\helpers\Db; |
|
50 | 51 | use craft\volumes\Temp; |
51 | 52 | use yii\base\Component; |
52 | 53 | use yii\base\InvalidArgumentException; |
| 54 | +use yii\base\InvalidConfigException; |
53 | 55 | use yii\base\NotSupportedException; |
54 | 56 |
|
55 | 57 | /** |
@@ -1001,6 +1003,54 @@ public function getCurrentUserTemporaryUploadFolder() |
1001 | 1003 | return $this->getUserTemporaryUploadFolder(); |
1002 | 1004 | } |
1003 | 1005 |
|
| 1006 | + /** |
| 1007 | + * Returns the temporary volume and subpath, if set. |
| 1008 | + * |
| 1009 | + * @return array |
| 1010 | + * @throws InvalidConfigException If the temp volume is invalid |
| 1011 | + * @since 3.7.39 |
| 1012 | + */ |
| 1013 | + public function getTempVolumeAndSubpath(): array |
| 1014 | + { |
| 1015 | + $assetSettings = Craft::$app->getProjectConfig()->get('assets'); |
| 1016 | + if (empty($assetSettings['tempVolumeUid'])) { |
| 1017 | + return [null, null]; |
| 1018 | + } |
| 1019 | + |
| 1020 | + $volume = Craft::$app->getVolumes()->getVolumeByUid($assetSettings['tempVolumeUid']); |
| 1021 | + |
| 1022 | + if (!$volume) { |
| 1023 | + throw new InvalidConfigException("The Temp Uploads Location is set to an invalid volume UID: {$assetSettings['tempVolumeUid']}"); |
| 1024 | + } |
| 1025 | + |
| 1026 | + $subpath = ($assetSettings['tempSubpath'] ?? null) ?: null; |
| 1027 | + return [$volume, $subpath]; |
| 1028 | + } |
| 1029 | + |
| 1030 | + /** |
| 1031 | + * Creates an asset query that is configured to return assets in the temporary upload location. |
| 1032 | + * |
| 1033 | + * @return AssetQuery |
| 1034 | + * @throws InvalidConfigException If the temp volume is invalid |
| 1035 | + * @since 3.7.39 |
| 1036 | + */ |
| 1037 | + public function createTempAssetQuery(): AssetQuery |
| 1038 | + { |
| 1039 | + /** @var VolumeInterface|null $volume */ |
| 1040 | + /** @var string|null $subpath */ |
| 1041 | + [$volume, $subpath] = $this->getTempVolumeAndSubpath(); |
| 1042 | + $query = Asset::find(); |
| 1043 | + if ($volume) { |
| 1044 | + $query->volumeId($volume->id); |
| 1045 | + if ($subpath) { |
| 1046 | + $query->folderPath("$subpath/*"); |
| 1047 | + } |
| 1048 | + } else { |
| 1049 | + $query->volumeId(':empty:'); |
| 1050 | + } |
| 1051 | + return $query; |
| 1052 | + } |
| 1053 | + |
1004 | 1054 | /** |
1005 | 1055 | * Returns the given user's temporary upload folder. |
1006 | 1056 | * |
@@ -1029,17 +1079,19 @@ public function getUserTemporaryUploadFolder(User $user = null) |
1029 | 1079 | } |
1030 | 1080 |
|
1031 | 1081 | // Is there a designated temp uploads volume? |
1032 | | - $assetSettings = Craft::$app->getProjectConfig()->get('assets'); |
1033 | | - if (isset($assetSettings['tempVolumeUid'])) { |
1034 | | - $volume = Craft::$app->getVolumes()->getVolumeByUid($assetSettings['tempVolumeUid']); |
1035 | | - if (!$volume) { |
1036 | | - throw new VolumeException(Craft::t('app', 'The volume set for temp asset storage is not valid.')); |
1037 | | - } |
1038 | | - $path = (isset($assetSettings['tempSubpath']) ? $assetSettings['tempSubpath'] . '/' : '') . |
1039 | | - $folderName; |
1040 | | - $folderId = $this->ensureFolderByFullPathAndVolume($path, $volume, false); |
| 1082 | + try { |
| 1083 | + /** @var VolumeInterface|null $tempVolume */ |
| 1084 | + /** @var string|null $tempSubpath */ |
| 1085 | + [$tempVolume, $tempSubpath] = $this->getTempVolumeAndSubpath(); |
| 1086 | + } catch (InvalidConfigException $e) { |
| 1087 | + throw new VolumeException($e->getMessage(), 0, $e); |
| 1088 | + } |
| 1089 | + |
| 1090 | + if ($tempVolume) { |
| 1091 | + $path = ($tempSubpath ? "$tempSubpath/" : '') . $folderName; |
| 1092 | + $folderId = $this->ensureFolderByFullPathAndVolume($path, $tempVolume, false); |
1041 | 1093 | return $this->findFolder([ |
1042 | | - 'volumeId' => $volume->id, |
| 1094 | + 'volumeId' => $tempVolume->id, |
1043 | 1095 | 'id' => $folderId, |
1044 | 1096 | ]); |
1045 | 1097 | } |
|
0 commit comments