2022-11-09 04:08:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2022 Varun Patil <radialapps@gmail.com>
|
|
|
|
* @author Varun Patil <radialapps@gmail.com>
|
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Memories\Controller;
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Exceptions;
|
2022-11-22 16:54:19 +00:00
|
|
|
use OCA\Memories\Exif;
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Util;
|
2022-11-09 04:08:30 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
2022-11-09 08:52:44 +00:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2022-11-22 11:35:20 +00:00
|
|
|
use OCP\Files\File;
|
2022-11-09 04:08:30 +00:00
|
|
|
|
2023-03-22 18:40:56 +00:00
|
|
|
class VideoController extends GenericApiController
|
2022-11-09 04:08:30 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
2022-12-03 06:23:43 +00:00
|
|
|
* @PublicPage
|
|
|
|
*
|
2022-11-09 04:08:30 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* Transcode a video to HLS by proxy
|
|
|
|
*/
|
2022-12-03 06:23:43 +00:00
|
|
|
public function transcode(string $client, int $fileid, string $profile): Http\Response
|
2022-11-09 04:08:30 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($client, $fileid, $profile) {
|
|
|
|
// Make sure not running in read-only mode
|
|
|
|
if (false !== $this->config->getSystemValue('memories.vod.disable', 'UNSET')) {
|
|
|
|
throw Exceptions::Forbidden('Transcoding disabled');
|
2023-02-24 08:21:38 +00:00
|
|
|
}
|
2023-03-23 20:32:23 +00:00
|
|
|
|
|
|
|
// Check client identifier is 8 characters or more
|
|
|
|
if (\strlen($client) < 8) {
|
|
|
|
throw Exceptions::MissingParameter('client (invalid)');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get file
|
|
|
|
$file = $this->getUserFile($fileid);
|
|
|
|
if (!$file || !$file->isReadable()) {
|
|
|
|
throw Exceptions::NotFoundFile($fileid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local files only for now
|
|
|
|
if (!$file->getStorage()->isLocal()) {
|
|
|
|
throw Exceptions::Forbidden('External storage not supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get file path
|
|
|
|
$path = $file->getStorage()->getLocalFile($file->getInternalPath());
|
|
|
|
if (!$path || !file_exists($path)) {
|
|
|
|
throw Exceptions::NotFound('local file path');
|
2023-02-24 08:21:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Check if file starts with temp dir
|
|
|
|
$tmpDir = sys_get_temp_dir();
|
|
|
|
if (0 === strpos($path, $tmpDir)) {
|
|
|
|
throw Exceptions::Forbidden('files in temp directory not supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request and check data was received
|
|
|
|
try {
|
|
|
|
$status = $this->getUpstream($client, $path, $profile);
|
|
|
|
if (409 === $status || -1 === $status) {
|
|
|
|
// Just a conflict (transcoding process changed)
|
|
|
|
return new JSONResponse(['message' => 'Conflict'], Http::STATUS_CONFLICT);
|
|
|
|
}
|
|
|
|
if (200 !== $status) {
|
|
|
|
throw new \Exception("Transcoder returned {$status}");
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$msg = 'Transcode failed: '.$e->getMessage();
|
|
|
|
$this->logger->error($msg, ['app' => 'memories']);
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
2022-11-09 04:08:30 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// The response was already streamed, so we have nothing to do here
|
|
|
|
exit;
|
|
|
|
});
|
2022-11-09 04:08:30 +00:00
|
|
|
}
|
2022-11-09 10:26:51 +00:00
|
|
|
|
2022-11-22 11:31:31 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
2022-12-03 05:53:30 +00:00
|
|
|
* @PublicPage
|
|
|
|
*
|
2022-11-22 11:31:31 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2023-03-10 18:09:52 +00:00
|
|
|
* Return the live video part of a Live Photo
|
2022-11-22 11:31:31 +00:00
|
|
|
*/
|
2022-12-03 04:44:24 +00:00
|
|
|
public function livephoto(
|
2022-12-03 05:53:30 +00:00
|
|
|
int $fileid,
|
2022-12-03 04:44:24 +00:00
|
|
|
string $liveid = '',
|
|
|
|
string $format = '',
|
|
|
|
string $transcode = ''
|
|
|
|
) {
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($fileid, $liveid, $format, $transcode) {
|
|
|
|
$file = $this->getUserFile($fileid);
|
|
|
|
if (null === $file) {
|
|
|
|
throw Exceptions::NotFoundFile($fileid);
|
2023-01-26 18:50:41 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Check file liveid
|
|
|
|
if (!$liveid) {
|
|
|
|
throw Exceptions::MissingParameter('liveid');
|
2022-11-22 16:54:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Response data
|
|
|
|
$name = '';
|
|
|
|
$mime = '';
|
|
|
|
$blob = null;
|
|
|
|
$liveVideoPath = null;
|
|
|
|
|
|
|
|
// Video is inside the file
|
|
|
|
$path = null;
|
|
|
|
if (str_starts_with($liveid, 'self__')) {
|
|
|
|
$path = $file->getStorage()->getLocalFile($file->getInternalPath());
|
|
|
|
$mime = 'video/mp4';
|
|
|
|
$name = $file->getName().'.mp4';
|
2022-11-22 16:54:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Different manufacurers have different formats
|
|
|
|
if ('self__trailer' === $liveid) {
|
|
|
|
try { // Get trailer
|
|
|
|
$blob = Exif::getBinaryExifProp($path, '-trailer');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw Exceptions::NotFound('file trailer');
|
|
|
|
}
|
|
|
|
} elseif ('self__embeddedvideo' === $liveid) {
|
|
|
|
try { // Get embedded video file
|
|
|
|
$blob = Exif::getBinaryExifProp($path, '-EmbeddedVideoFile');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw Exceptions::NotFound('embedded video');
|
|
|
|
}
|
|
|
|
} elseif (str_starts_with($liveid, 'self__traileroffset=')) {
|
|
|
|
// Remove prefix
|
|
|
|
$offset = (int) substr($liveid, \strlen('self__traileroffset='));
|
|
|
|
if ($offset <= 0) {
|
|
|
|
throw Exceptions::BadRequest('Invalid offset');
|
2022-11-22 17:19:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Read file from offset to end
|
|
|
|
$blob = file_get_contents($path, false, null, $offset);
|
|
|
|
} else {
|
|
|
|
// Get stored video file (Apple MOV)
|
|
|
|
$lp = $this->timelineQuery->getLivePhoto($fileid);
|
|
|
|
if (!$lp || $lp['liveid'] !== $liveid) {
|
|
|
|
throw Exceptions::NotFound('live video entry');
|
|
|
|
}
|
2022-11-22 11:35:20 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Get and return file
|
|
|
|
$liveFileId = (int) $lp['fileid'];
|
|
|
|
$files = $this->rootFolder->getById($liveFileId);
|
|
|
|
if (0 === \count($files)) {
|
|
|
|
throw Exceptions::NotFound('live video file');
|
|
|
|
}
|
|
|
|
$liveFile = $files[0];
|
|
|
|
|
|
|
|
if ($liveFile instanceof File) {
|
|
|
|
// Requested only JSON info
|
|
|
|
if ('json' === $format) {
|
|
|
|
return new JSONResponse($lp);
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = $liveFile->getName();
|
|
|
|
$blob = $liveFile->getContent();
|
|
|
|
$mime = $liveFile->getMimeType();
|
|
|
|
$liveVideoPath = $liveFile->getStorage()->getLocalFile($liveFile->getInternalPath());
|
2023-03-17 21:11:18 +00:00
|
|
|
}
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Data not found
|
|
|
|
if (!$blob) {
|
|
|
|
throw Exceptions::NotFound('live video data');
|
|
|
|
}
|
2023-02-11 16:19:46 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Transcode video if allowed
|
|
|
|
if ($transcode && !$this->config->getSystemValue('memories.vod.disable', true)) {
|
|
|
|
try {
|
|
|
|
// If video path not given, write to temp file
|
|
|
|
if (!$liveVideoPath) {
|
|
|
|
$liveVideoPath = self::postFile($transcode, $blob)['path'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is H.264 it won't get transcoded anyway
|
|
|
|
if ($liveVideoPath && 200 === $this->getUpstream($transcode, $liveVideoPath, 'max.mov')) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// Transcoding failed, just return the original video
|
2023-03-17 21:11:18 +00:00
|
|
|
}
|
2023-02-11 16:19:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Make and send response
|
|
|
|
$response = new DataDisplayResponse($blob, Http::STATUS_OK, []);
|
|
|
|
$response->setHeaders([
|
|
|
|
'Content-Type' => $mime,
|
|
|
|
'Content-Disposition' => "attachment; filename=\"{$name}\"",
|
|
|
|
]);
|
|
|
|
$response->cacheFor(3600 * 24, false, false);
|
2022-11-23 09:49:43 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
return $response;
|
|
|
|
});
|
2022-11-22 11:31:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 20:40:02 +00:00
|
|
|
/**
|
|
|
|
* Start the transcoder.
|
|
|
|
*
|
|
|
|
* @return string Path to log file
|
|
|
|
*/
|
|
|
|
public static function startGoVod()
|
2022-11-29 22:16:59 +00:00
|
|
|
{
|
2023-03-09 20:40:02 +00:00
|
|
|
$config = \OC::$server->get(\OCP\IConfig::class);
|
2022-11-29 22:16:59 +00:00
|
|
|
|
|
|
|
// Get transcoder path
|
2023-03-09 20:40:02 +00:00
|
|
|
$transcoder = $config->getSystemValue('memories.vod.path', false);
|
2022-11-29 22:16:59 +00:00
|
|
|
if (!$transcoder) {
|
2023-02-24 08:21:38 +00:00
|
|
|
throw new \Exception('Transcoder not configured');
|
2022-11-29 22:16:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 04:46:20 +00:00
|
|
|
// Make sure transcoder exists
|
|
|
|
if (!file_exists($transcoder)) {
|
|
|
|
throw new \Exception("Transcoder not found; run occ memories video-setup! ({$transcoder})");
|
|
|
|
}
|
|
|
|
|
2022-11-29 22:16:59 +00:00
|
|
|
// Make transcoder executable
|
|
|
|
if (!is_executable($transcoder)) {
|
|
|
|
@chmod($transcoder, 0755);
|
2023-02-24 08:21:38 +00:00
|
|
|
if (!is_executable($transcoder)) {
|
|
|
|
throw new \Exception("Transcoder not executable (chmod 755 {$transcoder})");
|
|
|
|
}
|
2022-11-29 22:16:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 07:26:49 +00:00
|
|
|
// Kill the transcoder in case it's running
|
|
|
|
\OCA\Memories\Util::pkill($transcoder);
|
|
|
|
|
2023-03-09 20:40:02 +00:00
|
|
|
// Start transcoder
|
|
|
|
[$configFile, $logFile] = self::makeGoVodConfig($config);
|
|
|
|
shell_exec("nohup {$transcoder} {$configFile} >> '{$logFile}' 2>&1 & > /dev/null");
|
2023-02-01 03:31:39 +00:00
|
|
|
|
2023-03-09 20:40:02 +00:00
|
|
|
// wait for 1s
|
|
|
|
sleep(1);
|
2023-02-24 07:26:49 +00:00
|
|
|
|
2023-03-09 20:40:02 +00:00
|
|
|
return $logFile;
|
|
|
|
}
|
2023-02-24 07:26:49 +00:00
|
|
|
|
2023-03-09 20:40:02 +00:00
|
|
|
/**
|
|
|
|
* Get the upstream URL for a video.
|
|
|
|
*/
|
|
|
|
public static function getGoVodUrl(string $client, string $path, string $profile): string
|
|
|
|
{
|
|
|
|
$config = \OC::$server->get(\OCP\IConfig::class);
|
|
|
|
$path = rawurlencode($path);
|
2023-03-17 16:32:43 +00:00
|
|
|
|
2023-03-12 21:58:10 +00:00
|
|
|
$bind = $config->getSystemValue('memories.vod.bind', '127.0.0.1:47788');
|
2023-03-17 16:32:43 +00:00
|
|
|
$connect = $config->getSystemValue('memories.vod.connect', $bind);
|
2023-02-24 07:26:49 +00:00
|
|
|
|
2023-03-17 16:32:43 +00:00
|
|
|
return "http://{$connect}/{$client}{$path}/{$profile}";
|
2023-03-09 20:40:02 +00:00
|
|
|
}
|
2023-02-24 08:21:38 +00:00
|
|
|
|
2023-03-17 16:50:09 +00:00
|
|
|
/**
|
|
|
|
* Get the goVod config JSON.
|
|
|
|
*/
|
|
|
|
public static function getGoVodConfig(\OCP\IConfig $config): array
|
|
|
|
{
|
|
|
|
// Migrate legacy config: remove in 2024
|
|
|
|
self::migrateLegacyConfig($config);
|
|
|
|
|
|
|
|
// Get temp directory
|
|
|
|
$defaultTmp = sys_get_temp_dir().'/go-vod/';
|
|
|
|
$tmpPath = $config->getSystemValue('memories.vod.tempdir', $defaultTmp);
|
|
|
|
|
|
|
|
// Make sure path ends with slash
|
|
|
|
if ('/' !== substr($tmpPath, -1)) {
|
|
|
|
$tmpPath .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add instance ID to path
|
|
|
|
$tmpPath .= $config->getSystemValue('instanceid', 'default');
|
|
|
|
|
|
|
|
// (Re-)create temp dir
|
|
|
|
shell_exec("rm -rf '{$tmpPath}' && mkdir -p '{$tmpPath}' && chmod 755 '{$tmpPath}'");
|
|
|
|
|
|
|
|
// Check temp directory exists
|
|
|
|
if (!is_dir($tmpPath)) {
|
|
|
|
throw new \Exception("Temp directory could not be created ({$tmpPath})");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check temp directory is writable
|
|
|
|
if (!is_writable($tmpPath)) {
|
|
|
|
throw new \Exception("Temp directory is not writable ({$tmpPath})");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get config from system values
|
|
|
|
return [
|
|
|
|
'bind' => $config->getSystemValue('memories.vod.bind', '127.0.0.1:47788'),
|
|
|
|
'ffmpeg' => $config->getSystemValue('memories.vod.ffmpeg', 'ffmpeg'),
|
|
|
|
'ffprobe' => $config->getSystemValue('memories.vod.ffprobe', 'ffprobe'),
|
|
|
|
'tempdir' => $tmpPath,
|
|
|
|
|
|
|
|
'vaapi' => $config->getSystemValue('memories.vod.vaapi', false),
|
|
|
|
'vaapiLowPower' => $config->getSystemValue('memories.vod.vaapi.low_power', false),
|
|
|
|
|
|
|
|
'nvenc' => $config->getSystemValue('memories.vod.nvenc', false),
|
|
|
|
'nvencTemporalAQ' => $config->getSystemValue('memories.vod.nvenc.temporal_aq', false),
|
|
|
|
'nvencScale' => $config->getSystemValue('memories.vod.nvenc.scale', 'npp'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-03-09 20:40:02 +00:00
|
|
|
private function getUpstream(string $client, string $path, string $profile)
|
|
|
|
{
|
|
|
|
$returnCode = $this->getUpstreamInternal($client, $path, $profile);
|
2023-03-17 17:52:44 +00:00
|
|
|
$isExternal = $this->config->getSystemValue('memories.vod.external', false);
|
2023-02-24 08:21:38 +00:00
|
|
|
|
2023-03-09 20:40:02 +00:00
|
|
|
// If status code was 0, it's likely the server is down
|
|
|
|
// Make one attempt to start after killing whatever is there
|
2023-03-17 17:52:44 +00:00
|
|
|
if (0 !== $returnCode || $isExternal) {
|
2023-03-09 20:40:02 +00:00
|
|
|
return $returnCode;
|
2023-02-24 08:21:38 +00:00
|
|
|
}
|
2023-02-01 03:31:39 +00:00
|
|
|
|
2023-03-09 20:40:02 +00:00
|
|
|
// Start goVod and get log file
|
|
|
|
$logFile = self::startGoVod();
|
2022-11-29 22:16:59 +00:00
|
|
|
|
2023-02-24 08:21:38 +00:00
|
|
|
$returnCode = $this->getUpstreamInternal($client, $path, $profile);
|
|
|
|
if (0 === $returnCode) {
|
|
|
|
throw new \Exception("Transcoder could not be started, check {$logFile}");
|
|
|
|
}
|
2023-02-24 08:22:21 +00:00
|
|
|
|
2023-02-24 08:21:38 +00:00
|
|
|
return $returnCode;
|
2022-11-29 22:16:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 20:40:02 +00:00
|
|
|
private function getUpstreamInternal(string $client, string $path, string $profile)
|
2022-11-09 10:42:42 +00:00
|
|
|
{
|
2022-12-03 06:23:43 +00:00
|
|
|
// Make sure query params are repeated
|
|
|
|
// For example, in folder sharing, we need the params on every request
|
2023-03-09 20:40:02 +00:00
|
|
|
$url = self::getGoVodUrl($client, $path, $profile);
|
2022-12-03 06:23:43 +00:00
|
|
|
if ($params = $_SERVER['QUERY_STRING']) {
|
|
|
|
$url .= "?{$params}";
|
|
|
|
}
|
|
|
|
|
|
|
|
$ch = curl_init($url);
|
2022-11-09 10:26:51 +00:00
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
2022-11-29 19:11:03 +00:00
|
|
|
|
2022-11-29 21:00:54 +00:00
|
|
|
// Catch connection abort here
|
|
|
|
ignore_user_abort(true);
|
|
|
|
|
2022-11-29 19:11:03 +00:00
|
|
|
// Stream the response to the browser without reading it into memory
|
|
|
|
$headersWritten = false;
|
2022-11-29 21:30:08 +00:00
|
|
|
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($curl, $data) use (&$headersWritten, $profile) {
|
2022-11-29 19:11:03 +00:00
|
|
|
$returnCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
|
|
|
|
2022-11-29 21:30:08 +00:00
|
|
|
if (200 === $returnCode) {
|
2022-11-29 19:11:03 +00:00
|
|
|
// Write headers if just got the first chunk of data
|
|
|
|
if (!$headersWritten) {
|
|
|
|
$headersWritten = true;
|
|
|
|
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
|
|
|
|
header("Content-Type: {$contentType}");
|
2022-11-29 21:30:08 +00:00
|
|
|
|
2022-11-29 22:21:34 +00:00
|
|
|
if (str_ends_with($profile, 'mov')) {
|
2022-11-29 21:30:08 +00:00
|
|
|
// cache full video 24 hours
|
|
|
|
header('Cache-Control: max-age=86400, public');
|
|
|
|
} else {
|
|
|
|
// no caching of segments
|
|
|
|
header('Cache-Control: no-cache, no-store, must-revalidate');
|
|
|
|
}
|
|
|
|
|
2022-11-29 21:00:54 +00:00
|
|
|
http_response_code($returnCode);
|
2022-11-29 19:11:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:30:08 +00:00
|
|
|
echo $data;
|
2022-11-29 19:11:03 +00:00
|
|
|
flush();
|
2022-11-29 21:00:54 +00:00
|
|
|
|
|
|
|
if (connection_aborted()) {
|
|
|
|
return -1; // stop the transfer
|
|
|
|
}
|
2022-11-29 19:11:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:30:08 +00:00
|
|
|
return \strlen($data);
|
2022-11-29 19:11:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Start the request
|
|
|
|
curl_exec($ch);
|
2022-11-09 10:26:51 +00:00
|
|
|
$returnCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
2022-11-09 10:42:42 +00:00
|
|
|
|
2022-11-29 19:11:03 +00:00
|
|
|
return $returnCode;
|
2022-11-09 10:26:51 +00:00
|
|
|
}
|
2023-03-09 20:40:02 +00:00
|
|
|
|
2023-03-17 21:11:18 +00:00
|
|
|
/**
|
|
|
|
* POST to go-vod to create a temporary file.
|
|
|
|
*
|
|
|
|
* @param mixed $blob
|
|
|
|
*/
|
|
|
|
private static function postFile(string $client, $blob)
|
|
|
|
{
|
|
|
|
$url = self::getGoVodUrl($client, '/create', 'ignore');
|
|
|
|
|
|
|
|
$ch = curl_init($url);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $blob);
|
|
|
|
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
$returnCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
if (200 !== $returnCode) {
|
|
|
|
throw new \Exception("Could not create temporary file ({$returnCode})");
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_decode($response, true);
|
|
|
|
}
|
|
|
|
|
2023-03-09 20:40:02 +00:00
|
|
|
/**
|
2023-03-17 16:50:09 +00:00
|
|
|
* Construct the goVod config JSON and put it to a file.
|
2023-03-09 20:40:02 +00:00
|
|
|
*
|
|
|
|
* @return array [config file, log file]
|
|
|
|
*/
|
|
|
|
private static function makeGoVodConfig(\OCP\IConfig $config): array
|
|
|
|
{
|
2023-03-17 16:50:09 +00:00
|
|
|
$env = self::getGoVodConfig($config);
|
|
|
|
$tmpPath = $env['tempdir'];
|
2023-03-09 20:40:02 +00:00
|
|
|
|
|
|
|
// Write config to file
|
|
|
|
$logFile = $tmpPath.'.log';
|
|
|
|
$configFile = $tmpPath.'.json';
|
|
|
|
file_put_contents($configFile, json_encode($env, JSON_PRETTY_PRINT));
|
|
|
|
|
|
|
|
// Log file is not in config
|
|
|
|
// go-vod just writes to stdout/stderr
|
|
|
|
return [$configFile, $logFile];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Migrate legacy config to new.
|
|
|
|
*
|
|
|
|
* Remove in year 2024
|
|
|
|
*/
|
|
|
|
private static function migrateLegacyConfig(\OCP\IConfig $config)
|
|
|
|
{
|
|
|
|
if (null === $config->getSystemValue('memories.no_transcode', null)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mapping
|
|
|
|
$legacyConfig = [
|
|
|
|
'memories.no_transcode' => 'memories.vod.disable',
|
|
|
|
'memories.transcoder' => 'memories.vod.path',
|
|
|
|
'memories.ffmpeg_path' => 'memories.vod.ffmpeg',
|
|
|
|
'memories.ffprobe_path' => 'memories.vod.ffprobe',
|
|
|
|
'memories.qsv' => 'memories.vod.vaapi',
|
|
|
|
'memories.nvenc' => 'memories.vod.nvenc',
|
|
|
|
'memories.tmp_path' => 'memories.vod.tempdir',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($legacyConfig as $old => $new) {
|
|
|
|
if (null !== $config->getSystemValue($old, null)) {
|
|
|
|
$config->setSystemValue($new, $config->getSystemValue($old));
|
|
|
|
$config->deleteSystemValue($old);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate bind address
|
|
|
|
if ($port = null !== $config->getSystemValue('memories.govod_port', null)) {
|
|
|
|
$config->setSystemValue('memories.vod.bind', "127.0.0.1:{$port}");
|
|
|
|
$config->deleteSystemValue('memories.govod_port');
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 04:08:30 +00:00
|
|
|
}
|