Skip to content

Commit 03ba8fd

Browse files
committed
Added new config parameter createSubfolders (bool). When turned on, subfolders will be created - if defined in the upload location of the original asset -
This way the path structure is more in sync with the asset path and the destination folder isn't flooded with single files.
1 parent 78c2995 commit 03ba8fd

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
// Use a md5 hash for the filenames instead of parameterized naming
5858
'useHashedNames' => false,
5959

60+
// if a upload location has a subfolder defined, add this to the transcoder paths too
61+
'createSubfolders' => true,
62+
6063
// Preset video encoders
6164
'videoEncoders' => [
6265
'h264' => [

src/services/Transcode.php

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,19 @@ public function getVideoUrl($filePath, $videoOptions, $generate = true): string
9595

9696
$result = '';
9797
$settings = Transcoder::$plugin->getSettings();
98+
$subfolder = '';
99+
100+
// sub folder check
101+
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
102+
$subfolder = $filePath->folderPath;
103+
}
104+
105+
// file path
98106
$filePath = $this->getAssetPath($filePath);
99-
107+
100108
if (!empty($filePath)) {
101-
$destVideoPath = $settings['transcoderPaths']['video'] ?? $settings['transcoderPaths']['default'];
102-
$destVideoPath = Craft::getAlias($destVideoPath);
109+
$destVideoPath = $settings['transcoderPaths']['video'] . $subfolder ?? $settings['transcoderPaths']['default'];
110+
$destVideoPath = Craft::getAlias($destVideoPath);
103111
$videoOptions = $this->coalesceOptions('defaultVideoOptions', $videoOptions);
104112

105113
// Get the video encoder presets to use
@@ -190,7 +198,7 @@ public function getVideoUrl($filePath, $videoOptions, $generate = true): string
190198

191199
// If the video file already exists and hasn't been modified, return it. Otherwise, start it transcoding
192200
if (file_exists($destVideoPath) && (@filemtime($destVideoPath) >= @filemtime($filePath))) {
193-
$url = $settings['transcoderUrls']['video'] ?? $settings['transcoderUrls']['default'];
201+
$url = $settings['transcoderUrls']['video'] . $subfolder ?? $settings['transcoderUrls']['default'];
194202
$result = Craft::getAlias($url).$destVideoFile;
195203

196204
// skip encoding
@@ -221,14 +229,20 @@ public function getVideoUrl($filePath, $videoOptions, $generate = true): string
221229
* @return string|false|null URL or path of the video thumbnail
222230
*/
223231
public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = true, $asPath = false)
224-
{
225-
232+
{
226233
$result = null;
227234
$settings = Transcoder::$plugin->getSettings();
235+
$subfolder = '';
236+
237+
// sub folder check
238+
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
239+
$subfolder = $filePath->folderPath;
240+
}
241+
228242
$filePath = $this->getAssetPath($filePath);
229243

230244
if (!empty($filePath)) {
231-
$destThumbnailPath = $settings['transcoderPaths']['thumbnail'] ?? $settings['transcoderPaths']['default'];
245+
$destThumbnailPath = $settings['transcoderPaths']['thumbnail'] . $subfolder ?? $settings['transcoderPaths']['default'];
232246
$destThumbnailPath = Craft::getAlias($destThumbnailPath);
233247

234248
$thumbnailOptions = $this->coalesceOptions('defaultThumbnailOptions', $thumbnailOptions);
@@ -289,7 +303,7 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = t
289303
if ($asPath) {
290304
$result = $destThumbnailPath;
291305
} else {
292-
$url = $settings['transcoderUrls']['thumbnail'] ?? $settings['transcoderUrls']['default'];
306+
$url = $settings['transcoderUrls']['thumbnail'] . $subfolder ?? $settings['transcoderUrls']['default'];
293307
$result = Craft::getAlias($url).$destThumbnailFile;
294308
}
295309
}
@@ -308,13 +322,19 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = t
308322
*/
309323
public function getAudioUrl($filePath, $audioOptions): string
310324
{
311-
312325
$result = '';
313326
$settings = Transcoder::$plugin->getSettings();
327+
$subfolder = '';
328+
329+
// sub folder check
330+
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
331+
$subfolder = $filePath->folderPath;
332+
}
333+
314334
$filePath = $this->getAssetPath($filePath);
315335

316336
if (!empty($filePath)) {
317-
$destAudioPath = $settings['transcoderPaths']['audio'] ?? $settings['transcoderPaths']['default'];
337+
$destAudioPath = $settings['transcoderPaths']['audio'] . $subfolder ?? $settings['transcoderPaths']['default'];
318338
$destAudioPath = Craft::getAlias($destAudioPath);
319339

320340
$audioOptions = $this->coalesceOptions('defaultAudioOptions', $audioOptions);
@@ -384,7 +404,7 @@ public function getAudioUrl($filePath, $audioOptions): string
384404

385405
// If the audio file already exists and hasn't been modified, return it. Otherwise, start it transcoding
386406
if (file_exists($destAudioPath) && (@filemtime($destAudioPath) >= @filemtime($filePath))) {
387-
$url = $settings['transcoderUrls']['audio'] ?? $settings['transcoderUrls']['default'];
407+
$url = $settings['transcoderUrls']['audio'] . $subfolder ?? $settings['transcoderUrls']['default'];
388408
$result = Craft::getAlias($url).$destAudioFile;
389409
} else {
390410
// Kick off the transcoding
@@ -409,7 +429,6 @@ public function getAudioUrl($filePath, $audioOptions): string
409429
*/
410430
public function getFileInfo($filePath, $summary = false): array
411431
{
412-
413432
$result = null;
414433
$settings = Transcoder::$plugin->getSettings();
415434
$filePath = $this->getAssetPath($filePath);
@@ -569,11 +588,18 @@ public function getGifUrl($filePath, $gifOptions): string
569588
{
570589
$result = '';
571590
$settings = Transcoder::$plugin->getSettings();
591+
$subfolder = '';
592+
593+
// sub folder check
594+
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
595+
$subfolder = $filePath->folderPath;
596+
}
597+
572598
$filePath = $this->getAssetPath($filePath);
573599

574600
if (!empty($filePath)) {
575601
// Dest path
576-
$destVideoPath = $settings['transcoderPaths']['gif'] ?? $settings['transcoderPaths']['default'];
602+
$destVideoPath = $settings['transcoderPaths']['gif'] . $subfolder ?? $settings['transcoderPaths']['default'];
577603
$destVideoPath = Craft::getAlias($destVideoPath);
578604

579605
// Options
@@ -627,7 +653,7 @@ public function getGifUrl($filePath, $gifOptions): string
627653

628654
// If the video file already exists and hasn't been modified, return it. Otherwise, start it transcoding
629655
if (file_exists($destVideoPath) && (@filemtime($destVideoPath) >= @filemtime($filePath))) {
630-
$url = $settings['transcoderUrls']['gif'] ?? $settings['transcoderUrls']['default'];
656+
$url = $settings['transcoderUrls']['gif'] . $subfolder ?? $settings['transcoderUrls']['default'];
631657
$result = Craft::getAlias($url).$destVideoFile;
632658
} else {
633659
// Kick off the transcoding

0 commit comments

Comments
 (0)