Skip to content

Commit

Permalink
Merge ed3556e into 139f196
Browse files Browse the repository at this point in the history
  • Loading branch information
internetztube committed Mar 24, 2019
2 parents 139f196 + ed3556e commit 1f93eac
Showing 1 changed file with 124 additions and 28 deletions.
152 changes: 124 additions & 28 deletions src/services/Transcode.php
Expand Up @@ -10,6 +10,8 @@

namespace nystudio107\transcoder\services;

use craft\records\VolumeFolder;
use craft\services\Assets;
use nystudio107\transcoder\Transcoder;

use Craft;
Expand Down Expand Up @@ -92,22 +94,21 @@ class Transcode extends Component
*/
public function getVideoUrl($filePath, $videoOptions, $generate = true): string
{

$result = '';
$settings = Transcoder::$plugin->getSettings();
$subfolder = '';

// sub folder check
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
$subfolder = $filePath->folderPath;
}

// file path
$filePath = $this->getAssetPath($filePath);

if (!empty($filePath)) {
$destVideoPath = $settings['transcoderPaths']['video'] . $subfolder ?? $settings['transcoderPaths']['default'];
$destVideoPath = Craft::getAlias($destVideoPath);
$destVideoPath = $this->localDestPath('video', $subfolder);
$videoOptions = $this->coalesceOptions('defaultVideoOptions', $videoOptions);

// Get the video encoder presets to use
Expand Down Expand Up @@ -197,10 +198,10 @@ public function getVideoUrl($filePath, $videoOptions, $generate = true): string
}

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

// skip encoding
} elseif (!$generate) {
$result = "";
Expand All @@ -212,6 +213,11 @@ public function getVideoUrl($filePath, $videoOptions, $generate = true): string
// Create a lockfile in tmp
file_put_contents($lockFile, $pid);
}

if ($remoteUrl = $this->moveAssetToVolume('video', $destVideoPath)) {
$result = $remoteUrl;
}
return $result;
}

return $result;
Expand All @@ -229,21 +235,20 @@ public function getVideoUrl($filePath, $videoOptions, $generate = true): string
* @return string|false|null URL or path of the video thumbnail
*/
public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = true, $asPath = false)
{
{
$result = null;
$settings = Transcoder::$plugin->getSettings();
$subfolder = '';

// sub folder check
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
$subfolder = $filePath->folderPath;
}

$filePath = $this->getAssetPath($filePath);

if (!empty($filePath)) {
$destThumbnailPath = $settings['transcoderPaths']['thumbnail'] . $subfolder ?? $settings['transcoderPaths']['default'];
$destThumbnailPath = Craft::getAlias($destThumbnailPath);
$destThumbnailPath = $this->localDestPath('thumbnail', $subfolder);

$thumbnailOptions = $this->coalesceOptions('defaultThumbnailOptions', $thumbnailOptions);

Expand Down Expand Up @@ -281,24 +286,29 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = t
$ffmpegCmd .= ' -f image2 -y '.escapeshellarg($destThumbnailPath).' >/dev/null 2>/dev/null &';

// If the thumbnail file already exists, return it. Otherwise, generate it and return it
if (!file_exists($destThumbnailPath)) {
if ($this->shouldGenerateAsset('thumbnail', $destThumbnailPath)) {
if ($generate) {
/** @noinspection PhpUnusedLocalVariableInspection */
$shellOutput = $this->executeShellCommand($ffmpegCmd);
Craft::info($ffmpegCmd, __METHOD__);

// if ffmpeg fails which we can't check because the process is ran in the background
// dont return the future path of the image or else we can't check this in the front end
// dont return the future path of the image or else we can't check this in the front end

return false;

return false;

} else {
Craft::info('Thumbnail does not exist, but not asked to generate it: '.$filePath, __METHOD__);

// The file doesn't exist, and we weren't asked to generate it
return false;
}
}

if ($remoteUrl = $this->moveAssetToVolume('thumbnail', $destThumbnailPath)) {
return $remoteUrl;
}

// Return either a path or a URL
if ($asPath) {
$result = $destThumbnailPath;
Expand All @@ -325,17 +335,16 @@ public function getAudioUrl($filePath, $audioOptions): string
$result = '';
$settings = Transcoder::$plugin->getSettings();
$subfolder = '';

// sub folder check
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
$subfolder = $filePath->folderPath;
}

$filePath = $this->getAssetPath($filePath);

if (!empty($filePath)) {
$destAudioPath = $settings['transcoderPaths']['audio'] . $subfolder ?? $settings['transcoderPaths']['default'];
$destAudioPath = Craft::getAlias($destAudioPath);
$destAudioPath = $this->localDestPath('audio', $subfolder);

$audioOptions = $this->coalesceOptions('defaultAudioOptions', $audioOptions);

Expand Down Expand Up @@ -403,7 +412,7 @@ public function getAudioUrl($filePath, $audioOptions): string
}

// If the audio file already exists and hasn't been modified, return it. Otherwise, start it transcoding
if (file_exists($destAudioPath) && (@filemtime($destAudioPath) >= @filemtime($filePath))) {
if ($this->shouldGenerateAsset('audio', $destAudioPath, $filePath)) {
$url = $settings['transcoderUrls']['audio'] . $subfolder ?? $settings['transcoderUrls']['default'];
$result = Craft::getAlias($url).$destAudioFile;
} else {
Expand All @@ -416,6 +425,10 @@ public function getAudioUrl($filePath, $audioOptions): string
}
}

if ($remoteUrl = $this->moveAssetToVolume('audio', $destAudioPath)) {
$result = $remoteUrl;
}

return $result;
}

Expand Down Expand Up @@ -591,18 +604,17 @@ public function getGifUrl($filePath, $gifOptions): string
$result = '';
$settings = Transcoder::$plugin->getSettings();
$subfolder = '';

// sub folder check
if(\is_object($filePath) && ($filePath instanceof Asset) && $settings['createSubfolders']) {
$subfolder = $filePath->folderPath;
}

$filePath = $this->getAssetPath($filePath);

if (!empty($filePath)) {
// Dest path
$destVideoPath = $settings['transcoderPaths']['gif'] . $subfolder ?? $settings['transcoderPaths']['default'];
$destVideoPath = Craft::getAlias($destVideoPath);
$destVideoPath = $this->localDestPath('gif', $subfolder);

// Options
$gifOptions = $this->coalesceOptions('defaultGifOptions', $gifOptions);
Expand Down Expand Up @@ -654,7 +666,7 @@ public function getGifUrl($filePath, $gifOptions): string
}

// If the video file already exists and hasn't been modified, return it. Otherwise, start it transcoding
if (file_exists($destVideoPath) && (@filemtime($destVideoPath) >= @filemtime($filePath))) {
if ($this->shouldGenerateAsset('gif', $destVideoPath, $filePath)) {
$url = $settings['transcoderUrls']['gif'] . $subfolder ?? $settings['transcoderUrls']['default'];
$result = Craft::getAlias($url).$destVideoFile;
} else {
Expand All @@ -665,6 +677,10 @@ public function getGifUrl($filePath, $gifOptions): string
// Create a lockfile in tmp
file_put_contents($lockFile, $pid);
}

if ($remoteUrl = $this->moveAssetToVolume('gif', $destVideoPath)) {
$result = $remoteUrl;
}
}

return $result;
Expand Down Expand Up @@ -874,4 +890,84 @@ protected function executeShellCommand(string $command): string

return $result;
}

protected function shouldGenerateAsset(string $type, string $destVideoFilePath, string $originalFilePath = null)
{
$settings = Transcoder::$plugin->getSettings();
$destSettings = $settings['transcoderPaths'][$type] ?? $settings['transcoderPaths']['default'];

if (file_exists($destVideoFilePath) && (@filemtime($destVideoFilePath) >= @filemtime($originalFilePath))) {
return false;
}

if (!is_array($destSettings)) {
return true;
}

$filename = basename($destVideoFilePath);

$volume = Craft::$app->getVolumes()->getVolumeByHandle($destSettings['volume']);
/** @var Asset $asset */
$asset = Asset::findOne(['volumeId' => $volume->id, 'filename' => $filename]);

if ($asset && $asset->dateModified->getTimestamp() >= @filemtime($originalFilePath)) {
return false;
}
return true;
}

protected function localDestPath(string $type, string $subfolder) {
$settings = Transcoder::$plugin->getSettings();
$destSettings = $settings['transcoderPaths'][$type] ?? $settings['transcoderPaths']['default'];
if (is_array($destSettings)) {
return sys_get_temp_dir();
}
if (isset($settings['transcoderPaths'][$type])) {
$destSettings .= $subfolder;
}
return Craft::getAlias($destSettings);
}

protected function moveAssetToVolume(string $type, string $filePath)
{
$settings = Transcoder::$plugin->getSettings();
$destSettings = $settings['transcoderPaths'][$type] ?? $settings['transcoderPaths']['default'];

// check if the setting is a volume
if (!is_array($destSettings) || empty($filePath)) {
return;
}

$volume = Craft::$app->getVolumes()->getVolumeByHandle($destSettings['volume']);
if (!$volume) {
$message = sprintf('Volume (handle: "%s") does not exist.', $destSettings['volume']);
throw new \RuntimeException($message);
}

$filename = basename($filePath);

/** @var Assets $assetService */
$assetElement = Asset::findOne(['volumeId' => $volume->id, 'filename' => $filename]);
if ($assetElement) {
return $assetElement->getUrl();
}

// check if the given folder exists
$volumeFolder = VolumeFolder::findOne(['path' => $destSettings['folder'], 'volumeId' => $volume->id]);
if (!$volumeFolder) {
$message = sprintf('Folder (path: "%s") in volume (handle: "%s") does not exist.', $destSettings['folder'], $destSettings['volume']);
throw new \RuntimeException($message);
}

$asset = new Asset();
$asset->volumeId = $volume->id;
$asset->tempFilePath = $filePath;
$asset->filename = $filename;
$asset->folderId = $volumeFolder->id;

Craft::$app->getElements()->saveElement($asset);
@unlink($filePath);

return $asset->getUrl();
}
}

0 comments on commit 1f93eac

Please sign in to comment.