memories/lib/Controller/VideoController.php

251 lines
9.1 KiB
PHP
Raw Normal View History

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;
use OCA\Memories\Exif;
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
class VideoController extends ApiBase
{
/**
* @NoAdminRequired
*
* @NoCSRFRequired
*
* Transcode a video to HLS by proxy
*/
2022-11-11 05:25:26 +00:00
public function transcode(string $client, string $fileid, string $profile): Http\Response
2022-11-09 04:08:30 +00:00
{
$user = $this->userSession->getUser();
if (null === $user) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
// Make sure not running in read-only mode
2022-11-09 10:42:42 +00:00
if (false !== $this->config->getSystemValue('memories.no_transcode', 'UNSET')) {
2022-11-09 04:08:30 +00:00
return new JSONResponse(['message' => 'Transcoding disabled'], Http::STATUS_FORBIDDEN);
}
2022-11-11 05:25:26 +00:00
// Check client identifier is 8 characters or more
if (\strlen($client) < 8) {
return new JSONResponse(['message' => 'Invalid client identifier'], Http::STATUS_BAD_REQUEST);
}
2022-11-09 04:08:30 +00:00
// Get file
2022-11-09 07:02:32 +00:00
$files = $this->rootFolder->getUserFolder($user->getUID())->getById($fileid);
2022-11-09 08:52:44 +00:00
if (0 === \count($files)) {
2022-11-09 04:08:30 +00:00
return new JSONResponse(['message' => 'File not found'], Http::STATUS_NOT_FOUND);
}
$file = $files[0];
2022-11-15 13:40:46 +00:00
if (!($file->getPermissions() & \OCP\Constants::PERMISSION_READ)) {
return new JSONResponse(['message' => 'File not readable'], Http::STATUS_FORBIDDEN);
}
2022-11-09 04:08:30 +00:00
// Local files only for now
if (!$file->getStorage()->isLocal()) {
return new JSONResponse(['message' => 'External storage not supported'], Http::STATUS_FORBIDDEN);
}
// Get file path
$path = $file->getStorage()->getLocalFile($file->getInternalPath());
if (!$path || !file_exists($path)) {
return new JSONResponse(['message' => 'File not found'], Http::STATUS_NOT_FOUND);
}
2022-11-09 10:26:51 +00:00
// Check if file starts with temp dir
$tmpDir = sys_get_temp_dir();
if (0 === strpos($path, $tmpDir)) {
return new JSONResponse(['message' => 'File is in temp dir!'], Http::STATUS_NOT_FOUND);
}
2022-11-09 04:08:30 +00:00
// Make upstream request
2022-11-11 05:25:26 +00:00
[$data, $contentType, $returnCode] = $this->getUpstream($client, $path, $profile);
2022-11-09 10:26:51 +00:00
// If status code was 0, it's likely the server is down
// Make one attempt to start if we can't find the process
if (0 === $returnCode) {
$transcoder = $this->config->getSystemValue('memories.transcoder', false);
2022-11-11 05:25:26 +00:00
if (!$transcoder) {
2022-11-09 10:26:51 +00:00
return new JSONResponse(['message' => 'Transcoder not configured'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
// Make transcoder executable
if (!is_executable($transcoder)) {
chmod($transcoder, 0755);
}
2022-11-09 14:21:17 +00:00
// Check for environment variables
$env = '';
2022-11-15 10:27:20 +00:00
// QSV with VAAPI
$vaapi = $this->config->getSystemValue('memories.qsv', false);
2022-11-15 10:39:15 +00:00
if ($vaapi) {
$env .= 'VAAPI=1 ';
}
2022-11-15 10:27:20 +00:00
// Paths
$ffmpegPath = $this->config->getSystemValue('memories.ffmpeg_path', 'ffmpeg');
$ffprobePath = $this->config->getSystemValue('memories.ffprobe_path', 'ffprobe');
$tmpPath = $this->config->getSystemValue('memories.tmp_path', sys_get_temp_dir());
2022-11-15 10:39:15 +00:00
$env .= "FFMPEG='{$ffmpegPath}' FFPROBE='{$ffprobePath}' GOVOD_TEMPDIR='{$tmpPath}/go-vod' ";
2022-11-09 14:21:17 +00:00
2022-11-09 10:26:51 +00:00
// Check if already running
2022-11-15 10:27:20 +00:00
exec("pkill {$transcoder}");
shell_exec("{$env} nohup {$transcoder} > {$tmpPath}/go-vod.log 2>&1 & > /dev/null");
2022-11-09 10:26:51 +00:00
2022-11-11 05:25:26 +00:00
// wait for 1s and try again
sleep(1);
[$data, $contentType, $returnCode] = $this->getUpstream($client, $path, $profile);
2022-11-09 10:26:51 +00:00
}
2022-11-09 04:08:30 +00:00
// Check data was received
if ($returnCode >= 400 || false === $data) {
return new JSONResponse(['message' => 'Transcode failed'], Http::STATUS_INTERNAL_SERVER_ERROR);
}
// Create and send response
$response = new DataDisplayResponse($data, Http::STATUS_OK, [
'Content-Type' => $contentType,
]);
2022-11-11 05:25:26 +00:00
$response->cacheFor(0, false, false);
2022-11-09 04:08:30 +00:00
return $response;
}
2022-11-09 10:26:51 +00:00
2022-11-22 11:31:31 +00:00
/**
* @NoAdminRequired
*
* @NoCSRFRequired
*
* Return the live video part of a live photo
*/
public function livephoto(string $fileid)
{
$fileid = (int) $fileid;
$files = $this->rootFolder->getById($fileid);
if (0 === \count($files)) {
return new JSONResponse(['message' => 'File not found'], Http::STATUS_NOT_FOUND);
}
$file = $files[0];
// Check file etag
$etag = $file->getEtag();
if ($etag !== $this->request->getParam('etag')) {
return new JSONResponse(['message' => 'File changed'], Http::STATUS_PRECONDITION_FAILED);
}
// Check file liveid
$liveid = $this->request->getParam('liveid');
if (!$liveid) {
return new JSONResponse(['message' => 'Live ID not provided'], Http::STATUS_BAD_REQUEST);
}
// Response data
$name = '';
$blob = null;
$mime = '';
// 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 11:35:20 +00:00
}
// Different manufacurers have different formats
if ('self__trailer' === $liveid) {
try { // Get trailer
$blob = Exif::getBinaryExifProp($path, '-trailer');
} catch (\Exception $e) {
return new JSONResponse(['message' => 'Trailer not found'], Http::STATUS_NOT_FOUND);
}
} elseif ('self__embeddedvideo' === $liveid) {
try { // Get embedded video file
$blob = Exif::getBinaryExifProp($path, '-EmbeddedVideoFile');
} catch (\Exception $e) {
return new JSONResponse(['message' => 'Embedded video not found'], Http::STATUS_NOT_FOUND);
}
} else {
// Get stored video file (Apple MOV)
$lp = $this->timelineQuery->getLivePhoto($fileid);
if (!$lp || $lp['liveid'] !== $liveid) {
return new JSONResponse(['message' => 'Live ID not found'], Http::STATUS_NOT_FOUND);
}
// Get and return file
$liveFileId = (int) $lp['fileid'];
$files = $this->rootFolder->getById($liveFileId);
if (0 === \count($files)) {
return new JSONResponse(['message' => 'Live file not found'], Http::STATUS_NOT_FOUND);
}
$liveFile = $files[0];
if ($liveFile instanceof File) {
2022-11-22 17:19:31 +00:00
// Requested only JSON info
if ('json' === $this->request->getParam('format')) {
return new JSONResponse($lp);
}
$name = $liveFile->getName();
$blob = $liveFile->getContent();
$mime = $liveFile->getMimeType();
}
}
2022-11-22 14:27:10 +00:00
// Make and send response
if ($blob) {
2022-11-22 14:27:10 +00:00
$response = new DataDisplayResponse($blob, Http::STATUS_OK, []);
$response->setHeaders([
'Content-Type' => $mime,
2022-11-22 14:30:24 +00:00
'Content-Disposition' => "attachment; filename=\"{$name}\"",
2022-11-22 11:35:20 +00:00
]);
$response->cacheFor(3600 * 24, false, false);
return $response;
}
return new JSONResponse(['message' => 'Live file not found'], Http::STATUS_NOT_FOUND);
2022-11-22 11:31:31 +00:00
}
2022-11-11 05:25:26 +00:00
private function getUpstream($client, $path, $profile)
2022-11-09 10:42:42 +00:00
{
2022-11-09 20:59:55 +00:00
$path = rawurlencode($path);
2022-11-11 05:25:26 +00:00
$ch = curl_init("http://127.0.0.1:47788/{$client}{$path}/{$profile}");
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);
$data = curl_exec($ch);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$returnCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
2022-11-09 10:42:42 +00:00
2022-11-09 10:26:51 +00:00
return [$data, $contentType, $returnCode];
}
2022-11-09 04:08:30 +00:00
}