2024-04-20 23:27:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes\Traits\Repairs;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2024-05-05 15:12:12 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-04-20 23:27:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait for AttachmentsManager
|
|
|
|
*/
|
|
|
|
trait RepairsVideos
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Repair all attached videos
|
|
|
|
*/
|
|
|
|
private function repairVideos()
|
|
|
|
{
|
|
|
|
$this->load();
|
|
|
|
|
|
|
|
foreach ($this->manager->get('files', []) as $ref => $data) {
|
|
|
|
$this->repairKnownVideo($ref, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Repair a single video. Move it to appropriate directory
|
|
|
|
*/
|
|
|
|
private function repairKnownVideo(string $ref, array $data)
|
|
|
|
{
|
|
|
|
if (!empty($data['url'])) {
|
|
|
|
$data['filename'] = $data['url'];
|
|
|
|
|
|
|
|
unset($data['url']);
|
|
|
|
|
|
|
|
$this->manager->set(sprintf('files.%s', $ref), $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
$extension = pathinfo($data['filename'], PATHINFO_EXTENSION);
|
|
|
|
$currentFullPath = sprintf('%s%s', $this->bundle->getDataDir(), $data['filename']);
|
|
|
|
$expectedFullPath = sprintf(
|
|
|
|
'%s%s/%s/%s/original.%s',
|
|
|
|
$this->bundle->getDataDir(),
|
|
|
|
$this->attachmentsDir,
|
|
|
|
$this->kind,
|
|
|
|
$ref,
|
|
|
|
$extension
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$this->disk->exists($currentFullPath)) {
|
|
|
|
$this->deleteAttachment($ref);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($data['last_modified'])) {
|
|
|
|
$data['last_modified'] = Carbon::parse($this->disk->lastModified($currentFullPath))->toIso8601String();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($currentFullPath !== $expectedFullPath) {
|
|
|
|
$this->disk->move($currentFullPath, $expectedFullPath);
|
|
|
|
|
|
|
|
$data['last_modified'] = Carbon::parse($this->disk->lastModified($expectedFullPath))->toIso8601String();
|
|
|
|
$data['filename'] = sprintf(
|
|
|
|
'%s/%s/%s/original.%s',
|
|
|
|
$this->attachmentsDir,
|
|
|
|
$this->kind,
|
|
|
|
$ref,
|
|
|
|
$extension
|
|
|
|
);
|
|
|
|
|
|
|
|
$parentDir = dirname($currentFullPath);
|
|
|
|
|
|
|
|
if (empty($this->disk->listContents($parentDir, true))) {
|
|
|
|
$this->disk->delete($parentDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-05 15:12:12 +02:00
|
|
|
$checksum = $this->disk->checksum($expectedFullPath);
|
|
|
|
|
|
|
|
if (!empty($data['checksum']) && $data['checksum'] !== $checksum) {
|
|
|
|
Log::warning('File checksum has changed', [
|
|
|
|
'file' => $expectedFullPath,
|
|
|
|
'oldChecsum' => $data['checksum'],
|
|
|
|
'newChecksum' => $checksum,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['checksum'] = $checksum;
|
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
$this->manager->set(sprintf('files.%s', $ref), $data);
|
|
|
|
}
|
|
|
|
}
|