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-04-20 20:04:42 +00:00
|
|
|
use OCA\Memories\HttpResponseException;
|
2023-04-14 23:02:13 +00:00
|
|
|
use OCA\Memories\Service\BinExt;
|
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
|
2023-03-23 21:45:56 +00:00
|
|
|
$file = $this->fs->getUserFile($fileid);
|
2023-03-23 20:32:23 +00:00
|
|
|
|
|
|
|
// 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();
|
2023-10-15 02:20:21 +00:00
|
|
|
if (str_starts_with($path, $tmpDir)) {
|
2023-03-23 20:32:23 +00:00
|
|
|
throw Exceptions::Forbidden('files in temp directory not supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request and check data was received
|
2023-10-15 19:46:35 +00:00
|
|
|
return Util::guardExDirect(function (Http\IOutput $out) use ($client, $path, $profile) {
|
2023-04-20 20:04:42 +00:00
|
|
|
try {
|
|
|
|
$status = $this->getUpstream($client, $path, $profile);
|
|
|
|
if (409 === $status || -1 === $status) {
|
|
|
|
// Just a conflict (transcoding process changed)
|
|
|
|
$response = new JSONResponse(['message' => 'Conflict'], Http::STATUS_CONFLICT);
|
2023-03-23 20:32:23 +00:00
|
|
|
|
2023-04-20 20:04:42 +00:00
|
|
|
throw new HttpResponseException($response);
|
|
|
|
}
|
|
|
|
if (200 !== $status) {
|
|
|
|
throw new \Exception("Transcoder returned {$status}");
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
if ($e instanceof HttpResponseException && Http::STATUS_CONFLICT === $e->response->getStatus()) {
|
|
|
|
throw $e; // Logging this is noise
|
|
|
|
}
|
2022-11-09 04:08:30 +00:00
|
|
|
|
2023-04-20 20:04:42 +00:00
|
|
|
// We cannot show this error in the user interface, so log it
|
|
|
|
$this->logger->error('Transcode failed: '.$e->getMessage(), ['app' => 'memories']);
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
});
|
2023-03-23 20:32:23 +00:00
|
|
|
});
|
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 = '',
|
2023-10-15 01:59:00 +00:00
|
|
|
string $transcode = '',
|
2023-10-14 08:25:50 +00:00
|
|
|
): Http\Response {
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($fileid, $liveid, $format, $transcode) {
|
2023-03-23 21:45:56 +00:00
|
|
|
$file = $this->fs->getUserFile($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
|
2023-10-15 20:16:57 +00:00
|
|
|
$path = '<>';
|
2023-03-23 20:32:23 +00:00
|
|
|
if (str_starts_with($liveid, 'self__')) {
|
2023-10-15 20:16:57 +00:00
|
|
|
$path = $file->getStorage()->getLocalFile($file->getInternalPath())
|
|
|
|
?: throw Exceptions::BadRequest('[Video] File path missing (self__*)');
|
2023-03-23 20:32:23 +00:00
|
|
|
$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');
|
2023-10-15 20:16:57 +00:00
|
|
|
} catch (\Exception) {
|
2023-03-23 20:32:23 +00:00
|
|
|
throw Exceptions::NotFound('file trailer');
|
|
|
|
}
|
2023-10-05 06:51:52 +00:00
|
|
|
} elseif (str_starts_with($liveid, 'self__exifbin=')) {
|
|
|
|
$field = substr($liveid, \strlen('self__exifbin='));
|
|
|
|
|
|
|
|
// Need explicit whitelisting here because this is user input
|
|
|
|
if (!\in_array($field, ['EmbeddedVideoFile', 'MotionPhotoVideo'], true)) {
|
|
|
|
throw Exceptions::BadRequest('Invalid binary EXIF field');
|
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
try { // Get embedded video file
|
2023-10-05 06:51:52 +00:00
|
|
|
$blob = Exif::getBinaryExifProp($path, "-{$field}");
|
2023-10-15 20:16:57 +00:00
|
|
|
} catch (\Exception) {
|
2023-10-05 06:51:52 +00:00
|
|
|
throw Exceptions::NotFound('Could not read binary EXIF field');
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
|
|
|
} 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 {
|
2023-04-25 21:50:48 +00:00
|
|
|
$liveFile = $this->getClosestLiveVideo($file);
|
|
|
|
if (null === $liveFile) {
|
2023-03-23 20:32:23 +00:00
|
|
|
throw Exceptions::NotFound('live video file');
|
|
|
|
}
|
|
|
|
|
2023-04-25 21:50:48 +00:00
|
|
|
// Requested only JSON info
|
|
|
|
if ('json' === $format) {
|
2023-08-29 17:27:26 +00:00
|
|
|
// IPhoto object for the live video
|
2023-04-25 21:50:48 +00:00
|
|
|
return new JSONResponse([
|
|
|
|
'fileid' => $liveFile->getId(),
|
2023-08-29 17:27:26 +00:00
|
|
|
'etag' => $liveFile->getEtag(),
|
|
|
|
'basename' => $liveFile->getName(),
|
|
|
|
'mimetype' => $liveFile->getMimeType(),
|
2023-04-25 21:50:48 +00:00
|
|
|
]);
|
2023-03-17 21:11:18 +00:00
|
|
|
}
|
2023-04-25 21:50:48 +00:00
|
|
|
|
|
|
|
$name = $liveFile->getName();
|
|
|
|
$blob = $liveFile->getContent();
|
|
|
|
$mime = $liveFile->getMimeType();
|
|
|
|
$liveVideoPath = $liveFile->getStorage()->getLocalFile($liveFile->getInternalPath());
|
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-04-25 20:48:25 +00:00
|
|
|
// Cannot return JSON if it is not a file
|
|
|
|
if ('json' === $format) {
|
|
|
|
throw Exceptions::BadRequest('Invalid format');
|
|
|
|
}
|
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Transcode video if allowed
|
|
|
|
if ($transcode && !$this->config->getSystemValue('memories.vod.disable', true)) {
|
2023-04-20 20:20:46 +00:00
|
|
|
// If video path not given, write to temp file
|
|
|
|
if (!$liveVideoPath) {
|
|
|
|
$liveVideoPath = self::postFile($transcode, $blob)['path'];
|
|
|
|
}
|
2023-03-23 20:32:23 +00:00
|
|
|
|
2023-04-20 20:20:46 +00:00
|
|
|
// If this is H.264 it won't get transcoded anyway
|
|
|
|
if ($liveVideoPath) {
|
|
|
|
return Util::guardExDirect(function ($out) use ($transcode, $liveVideoPath) {
|
2023-09-30 18:37:40 +00:00
|
|
|
$this->getUpstream($transcode, $liveVideoPath, 'max.mp4');
|
2023-04-20 20:20:46 +00:00
|
|
|
});
|
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-10-14 08:25:50 +00:00
|
|
|
private function getUpstream(string $client, string $path, string $profile): int
|
2023-03-09 20:40:02 +00:00
|
|
|
{
|
|
|
|
$returnCode = $this->getUpstreamInternal($client, $path, $profile);
|
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-04-10 23:45:04 +00:00
|
|
|
if (0 !== $returnCode && 503 !== $returnCode) {
|
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
|
2023-04-10 23:45:04 +00:00
|
|
|
$logFile = BinExt::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-10-14 08:25:50 +00:00
|
|
|
private function getUpstreamInternal(string $client, string $path, string $profile): int
|
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-04-10 23:45:04 +00:00
|
|
|
$url = BinExt::getGoVodUrl($client, $path, $profile);
|
2023-10-15 19:46:35 +00:00
|
|
|
if (\array_key_exists('QUERY_STRING', $_SERVER) && !empty($params = $_SERVER['QUERY_STRING'])) {
|
2022-12-03 06:23:43 +00:00
|
|
|
$url .= "?{$params}";
|
|
|
|
}
|
|
|
|
|
2023-10-20 21:28:08 +00:00
|
|
|
// Initialize request
|
2022-12-03 06:23:43 +00:00
|
|
|
$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
|
|
|
|
2023-10-20 21:28:08 +00:00
|
|
|
// Add header for expected go-vod version
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-Go-Vod-Version: '.BinExt::GOVOD_VER]);
|
|
|
|
|
2022-11-29 21:00:54 +00:00
|
|
|
// Catch connection abort here
|
|
|
|
ignore_user_abort(true);
|
|
|
|
|
2023-10-31 20:04:02 +00:00
|
|
|
$headerswritten = false;
|
|
|
|
$sendheaders = static function (\CurlHandle $curl) use (&$headerswritten, $profile): void {
|
|
|
|
// Write headers if just got the first chunk of data
|
|
|
|
if ($headerswritten) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$headerswritten = true;
|
2022-11-29 21:30:08 +00:00
|
|
|
|
2023-10-31 20:04:02 +00:00
|
|
|
// Pass ahead response code
|
|
|
|
http_response_code((int) curl_getinfo($curl, CURLINFO_HTTP_CODE));
|
2022-11-29 19:11:03 +00:00
|
|
|
|
2023-10-31 20:04:02 +00:00
|
|
|
// Pass ahead content type
|
|
|
|
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
|
|
|
|
header("Content-Type: {$contentType}");
|
2022-11-29 21:00:54 +00:00
|
|
|
|
2023-10-31 20:04:02 +00:00
|
|
|
// Caching headers
|
|
|
|
if (str_ends_with($profile, 'mp4')) {
|
|
|
|
// 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 19:11:03 +00:00
|
|
|
}
|
2023-10-31 20:04:02 +00:00
|
|
|
};
|
2022-11-29 19:11:03 +00:00
|
|
|
|
2023-10-31 20:04:02 +00:00
|
|
|
// On Safari with MP4, chunked transfer encoding is not supported
|
|
|
|
// So we need to read the whole file into memory and send it
|
|
|
|
$userAgent = $this->request->getHeader('User-Agent');
|
|
|
|
$isSafari = preg_match('/^((?!chrome|android).)*safari/i', $userAgent);
|
|
|
|
|
|
|
|
if (!$isSafari) {
|
|
|
|
// Stream the response to the browser without reading it into memory
|
|
|
|
curl_setopt($ch, CURLOPT_WRITEFUNCTION, static function (\CurlHandle $curl, string $data) use (&$sendheaders) {
|
|
|
|
$code = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
|
|
|
|
|
|
|
if (200 === $code) {
|
|
|
|
// Write headers if not done yet
|
|
|
|
$sendheaders($curl);
|
|
|
|
|
|
|
|
// Chunked transfer encoding
|
|
|
|
echo $data;
|
|
|
|
flush();
|
|
|
|
|
|
|
|
// Check if the client is still connected
|
|
|
|
if (connection_aborted()) {
|
|
|
|
return -1; // stop the transfer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return \strlen($data);
|
|
|
|
});
|
|
|
|
}
|
2022-11-29 19:11:03 +00:00
|
|
|
|
|
|
|
// Start the request
|
2023-10-31 20:04:02 +00:00
|
|
|
$response = curl_exec($ch);
|
|
|
|
|
|
|
|
// Send the entire response if Safari
|
|
|
|
if ($isSafari && \is_string($response)) {
|
|
|
|
$sendheaders($ch);
|
|
|
|
header('Content-Length: '.\strlen($response)); // critical
|
|
|
|
echo $response;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2023-10-15 19:46:35 +00:00
|
|
|
* @return mixed The response from upstream
|
2023-03-17 21:11:18 +00:00
|
|
|
*/
|
2023-10-15 20:16:57 +00:00
|
|
|
private static function postFile(string $client, string $blob): mixed
|
2023-04-20 21:03:18 +00:00
|
|
|
{
|
2023-04-20 20:20:46 +00:00
|
|
|
try {
|
|
|
|
return self::postFileInternal($client, $blob);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
if (BinExt::startGoVod()) { // If the server is down, try to start it
|
|
|
|
return self::postFileInternal($client, $blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
private static function postFileInternal(string $client, mixed $blob): mixed
|
2023-03-17 21:11:18 +00:00
|
|
|
{
|
2023-04-10 23:45:04 +00:00
|
|
|
$url = BinExt::getGoVodUrl($client, '/create', 'ignore');
|
2023-03-17 21:11:18 +00:00
|
|
|
|
|
|
|
$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})");
|
|
|
|
}
|
|
|
|
|
2023-10-15 19:46:35 +00:00
|
|
|
return json_decode((string) $response, true);
|
2023-03-17 21:11:18 +00:00
|
|
|
}
|
2023-04-25 21:50:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the closest live video to the given file.
|
|
|
|
*/
|
|
|
|
private function getClosestLiveVideo(File $file): ?File
|
|
|
|
{
|
|
|
|
// Get stored video file (Apple MOV)
|
2023-10-14 06:51:12 +00:00
|
|
|
$liveRecords = $this->tq->getLivePhotos($file->getId());
|
2023-04-25 21:50:48 +00:00
|
|
|
|
|
|
|
// Get file paths for all live photos
|
|
|
|
$liveFiles = array_map(fn ($r) => $this->rootFolder->getById((int) $r['fileid']), $liveRecords);
|
2023-08-30 18:10:08 +00:00
|
|
|
$liveFiles = array_filter($liveFiles, static fn ($files) => \count($files) > 0 && $files[0] instanceof File);
|
2023-10-15 19:46:35 +00:00
|
|
|
|
|
|
|
/** @var File[] (checked above) */
|
2023-08-30 18:10:08 +00:00
|
|
|
$liveFiles = array_map(static fn ($files) => $files[0], $liveFiles);
|
2023-04-25 21:50:48 +00:00
|
|
|
|
|
|
|
// Should be filtered enough by now
|
|
|
|
if (!\count($liveFiles)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All paths including the image and videos need to be processed
|
2023-08-30 18:10:08 +00:00
|
|
|
$paths = array_map(static function (File $file) {
|
2023-04-25 21:50:48 +00:00
|
|
|
$path = $file->getPath();
|
|
|
|
$filename = strtolower($file->getName());
|
|
|
|
|
|
|
|
// Remove extension so the filename itself counts in the path
|
|
|
|
if (str_contains($filename, '.')) {
|
2023-10-15 19:46:35 +00:00
|
|
|
$filename = substr($filename, 0, strrpos($filename, '.') ?: null);
|
2023-04-25 21:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get components with the filename as lowercase
|
|
|
|
$components = explode('/', $path);
|
|
|
|
if (($l = \count($components)) > 0) {
|
|
|
|
$components[$l - 1] = $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $components;
|
|
|
|
}, array_merge($liveFiles, [$file]));
|
|
|
|
|
|
|
|
// Find closest path match
|
|
|
|
$imagePath = array_pop($paths);
|
2023-08-30 18:10:08 +00:00
|
|
|
$scores = array_map(static function ($path) use ($imagePath) {
|
2023-04-25 21:50:48 +00:00
|
|
|
$score = 0;
|
|
|
|
$length = min(\count($path), \count($imagePath));
|
|
|
|
for ($i = 0; $i < $length; ++$i) {
|
|
|
|
if ($path[$i] === $imagePath[$i]) {
|
|
|
|
$score += 10000; // Exact match bonus
|
|
|
|
} else {
|
|
|
|
$score -= \count($path) - $i; // Walk down penalty
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $score;
|
|
|
|
}, $paths);
|
|
|
|
|
|
|
|
// Sort by score
|
|
|
|
array_multisort($scores, SORT_ASC, $liveFiles);
|
|
|
|
|
|
|
|
return array_pop($liveFiles);
|
|
|
|
}
|
2022-11-09 04:08:30 +00:00
|
|
|
}
|