mirror of
https://code.castopod.org/adaures/castopod
synced 2025-05-22 10:02:01 +00:00
fix(import): save media files during podcast import + set missing media fields
This commit is contained in:
parent
58e2a00a87
commit
a9989d841a
@ -51,6 +51,7 @@ class AddMedia extends Migration
|
|||||||
'language_code' => [
|
'language_code' => [
|
||||||
'type' => 'VARCHAR',
|
'type' => 'VARCHAR',
|
||||||
'constraint' => 2,
|
'constraint' => 2,
|
||||||
|
'null' => true,
|
||||||
],
|
],
|
||||||
'uploaded_by' => [
|
'uploaded_by' => [
|
||||||
'type' => 'INT',
|
'type' => 'INT',
|
||||||
|
@ -22,6 +22,7 @@ use App\Models\PersonModel;
|
|||||||
use App\Models\PodcastModel;
|
use App\Models\PodcastModel;
|
||||||
use App\Models\PostModel;
|
use App\Models\PostModel;
|
||||||
use CodeIgniter\Entity\Entity;
|
use CodeIgniter\Entity\Entity;
|
||||||
|
use CodeIgniter\Files\File;
|
||||||
use CodeIgniter\HTTP\Files\UploadedFile;
|
use CodeIgniter\HTTP\Files\UploadedFile;
|
||||||
use CodeIgniter\I18n\Time;
|
use CodeIgniter\I18n\Time;
|
||||||
use League\CommonMark\CommonMarkConverter;
|
use League\CommonMark\CommonMarkConverter;
|
||||||
@ -165,9 +166,9 @@ class Episode extends Entity
|
|||||||
'updated_by' => 'integer',
|
'updated_by' => 'integer',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function setCover(?UploadedFile $file): self
|
public function setCover(UploadedFile | File $file = null): self
|
||||||
{
|
{
|
||||||
if ($file === null || ! $file->isValid()) {
|
if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,9 +213,9 @@ class Episode extends Entity
|
|||||||
return $this->cover;
|
return $this->cover;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setAudio(?UploadedFile $file): self
|
public function setAudio(UploadedFile | File $file = null): self
|
||||||
{
|
{
|
||||||
if ($file === null || ! $file->isValid()) {
|
if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,6 +229,8 @@ class Episode extends Entity
|
|||||||
$audio = new Audio([
|
$audio = new Audio([
|
||||||
'file_name' => $this->attributes['slug'],
|
'file_name' => $this->attributes['slug'],
|
||||||
'file_directory' => 'podcasts/' . $this->getPodcast()->handle,
|
'file_directory' => 'podcasts/' . $this->getPodcast()->handle,
|
||||||
|
'language_code' => $this->getPodcast()
|
||||||
|
->language_code,
|
||||||
'uploaded_by' => user_id(),
|
'uploaded_by' => user_id(),
|
||||||
'updated_by' => user_id(),
|
'updated_by' => user_id(),
|
||||||
]);
|
]);
|
||||||
@ -248,9 +251,9 @@ class Episode extends Entity
|
|||||||
return $this->audio;
|
return $this->audio;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTranscript(?UploadedFile $file): self
|
public function setTranscript(UploadedFile | File $file = null): self
|
||||||
{
|
{
|
||||||
if ($file === null || ! $file->isValid()) {
|
if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,6 +267,8 @@ class Episode extends Entity
|
|||||||
$transcript = new Transcript([
|
$transcript = new Transcript([
|
||||||
'file_name' => $this->attributes['slug'] . '-transcript',
|
'file_name' => $this->attributes['slug'] . '-transcript',
|
||||||
'file_directory' => 'podcasts/' . $this->getPodcast()->handle,
|
'file_directory' => 'podcasts/' . $this->getPodcast()->handle,
|
||||||
|
'language_code' => $this->getPodcast()
|
||||||
|
->language_code,
|
||||||
'uploaded_by' => user_id(),
|
'uploaded_by' => user_id(),
|
||||||
'updated_by' => user_id(),
|
'updated_by' => user_id(),
|
||||||
]);
|
]);
|
||||||
@ -284,9 +289,9 @@ class Episode extends Entity
|
|||||||
return $this->transcript;
|
return $this->transcript;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setChapters(?UploadedFile $file): self
|
public function setChapters(UploadedFile | File $file = null): self
|
||||||
{
|
{
|
||||||
if ($file === null || ! $file->isValid()) {
|
if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,6 +305,8 @@ class Episode extends Entity
|
|||||||
$chapters = new Chapters([
|
$chapters = new Chapters([
|
||||||
'file_name' => $this->attributes['slug'] . '-chapters',
|
'file_name' => $this->attributes['slug'] . '-chapters',
|
||||||
'file_directory' => 'podcasts/' . $this->getPodcast()->handle,
|
'file_directory' => 'podcasts/' . $this->getPodcast()->handle,
|
||||||
|
'language_code' => $this->getPodcast()
|
||||||
|
->language_code,
|
||||||
'uploaded_by' => user_id(),
|
'uploaded_by' => user_id(),
|
||||||
'updated_by' => user_id(),
|
'updated_by' => user_id(),
|
||||||
]);
|
]);
|
||||||
|
@ -44,8 +44,8 @@ class Audio extends BaseMedia
|
|||||||
$this->attributes['file_mimetype'] = $audioMetadata['mime_type'];
|
$this->attributes['file_mimetype'] = $audioMetadata['mime_type'];
|
||||||
$this->attributes['file_size'] = $audioMetadata['filesize'];
|
$this->attributes['file_size'] = $audioMetadata['filesize'];
|
||||||
// @phpstan-ignore-next-line
|
// @phpstan-ignore-next-line
|
||||||
$this->attributes['description'] = @$audioMetadata['id3v2']['comments']['comment'];
|
$this->attributes['description'] = @$audioMetadata['id3v2']['comments']['comment'][0];
|
||||||
$this->attributes['file_metadata'] = json_encode($audioMetadata);
|
$this->attributes['file_metadata'] = json_encode($audioMetadata, JSON_INVALID_UTF8_SUBSTITUTE);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -88,6 +88,7 @@ class BaseMedia extends Entity
|
|||||||
{
|
{
|
||||||
helper('media');
|
helper('media');
|
||||||
|
|
||||||
|
$this->attributes['type'] = $this->type;
|
||||||
$this->attributes['file_mimetype'] = $file->getMimeType();
|
$this->attributes['file_mimetype'] = $file->getMimeType();
|
||||||
$this->attributes['file_metadata'] = json_encode(lstat((string) $file));
|
$this->attributes['file_metadata'] = json_encode(lstat((string) $file));
|
||||||
$this->attributes['file_path'] = save_media(
|
$this->attributes['file_path'] = save_media(
|
||||||
|
@ -54,14 +54,21 @@ class Image extends BaseMedia
|
|||||||
{
|
{
|
||||||
parent::setFile($file);
|
parent::setFile($file);
|
||||||
|
|
||||||
$metadata = exif_read_data(media_path($this->file_path), null, true);
|
if ($this->file_mimetype === 'image/jpeg' && $metadata = exif_read_data(
|
||||||
|
media_path($this->file_path),
|
||||||
if ($metadata) {
|
null,
|
||||||
|
true
|
||||||
|
)) {
|
||||||
$metadata['sizes'] = $this->sizes;
|
$metadata['sizes'] = $this->sizes;
|
||||||
$this->attributes['file_size'] = $metadata['FILE']['FileSize'];
|
$this->attributes['file_size'] = $metadata['FILE']['FileSize'];
|
||||||
$this->attributes['file_metadata'] = json_encode($metadata);
|
} else {
|
||||||
|
$metadata = [
|
||||||
|
'sizes' => $this->sizes,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->attributes['file_metadata'] = json_encode($metadata);
|
||||||
|
|
||||||
$this->initFileProperties();
|
$this->initFileProperties();
|
||||||
$this->saveSizes();
|
$this->saveSizes();
|
||||||
|
|
||||||
|
@ -54,11 +54,6 @@ class EpisodeModel extends Model
|
|||||||
*/
|
*/
|
||||||
protected $table = 'episodes';
|
protected $table = 'episodes';
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $primaryKey = 'id';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string[]
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
|
@ -32,6 +32,23 @@ class MediaModel extends Model
|
|||||||
*/
|
*/
|
||||||
protected $returnType = Document::class;
|
protected $returnType = Document::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $useSoftDeletes = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $useTimestamps = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The column used for insert timestamps
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $createdField = 'uploaded_at';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string[]
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
|
@ -345,7 +345,7 @@ class PodcastImportController extends BaseController
|
|||||||
'title' => $item->title,
|
'title' => $item->title,
|
||||||
'slug' => $slug,
|
'slug' => $slug,
|
||||||
'guid' => $item->guid ?? null,
|
'guid' => $item->guid ?? null,
|
||||||
'audio_file' => download_file(
|
'audio' => download_file(
|
||||||
(string) $item->enclosure->attributes()['url'],
|
(string) $item->enclosure->attributes()['url'],
|
||||||
(string) $item->enclosure->attributes()['type']
|
(string) $item->enclosure->attributes()['type']
|
||||||
),
|
),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user