Include original media in media groups
This commit is contained in:
parent
89d462e280
commit
954608c5f9
3 changed files with 212 additions and 14 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import hashlib
|
||||
import logging
|
||||
import mimetypes
|
||||
import tempfile
|
||||
import time
|
||||
from io import BytesIO
|
||||
|
|
@ -116,6 +117,26 @@ class TranscodePipeline(BaseFilesPipeline):
|
|||
for index, setting in enumerate(settings)
|
||||
]
|
||||
|
||||
def original_path(self, source_url: str) -> str:
|
||||
if self.media_type == repub.utils.FileType.AUDIO:
|
||||
return repub.utils.local_audio_path(source_url)
|
||||
if self.media_type == repub.utils.FileType.VIDEO:
|
||||
return repub.utils.local_video_path(source_url)
|
||||
raise ValueError(f"Unsupported media type: {self.media_type}")
|
||||
|
||||
def original_mimetype(self, source_url: str, response=None) -> str:
|
||||
if response is not None:
|
||||
content_type = response.headers.get(b"Content-Type")
|
||||
if content_type:
|
||||
return content_type.decode("utf-8").split(";", 1)[0].strip()
|
||||
mimetype = mimetypes.guess_type(source_url)[0]
|
||||
if mimetype:
|
||||
return mimetype
|
||||
return {
|
||||
repub.utils.FileType.AUDIO: "audio/mpeg",
|
||||
repub.utils.FileType.VIDEO: "video/mp4",
|
||||
}[self.media_type]
|
||||
|
||||
def published_url(self, path: str, item=None) -> str:
|
||||
relative_path = f"{self.media_dir()}/{path}"
|
||||
feed_url = str(self.settings.get("REPUBLISHER_FEED_URL", "")).rstrip("/")
|
||||
|
|
@ -130,7 +151,7 @@ class TranscodePipeline(BaseFilesPipeline):
|
|||
self,
|
||||
*,
|
||||
path: str,
|
||||
setting: media.MediaSettings,
|
||||
mimetype: str,
|
||||
probe_result: dict[str, Any],
|
||||
is_default: bool,
|
||||
item=None,
|
||||
|
|
@ -138,7 +159,7 @@ class TranscodePipeline(BaseFilesPipeline):
|
|||
variant: MediaVariant = {
|
||||
"url": self.published_url(path, item),
|
||||
"path": path,
|
||||
"type": setting["mimetype"],
|
||||
"type": mimetype,
|
||||
"medium": self.media_type.value,
|
||||
"isDefault": "true" if is_default else "false",
|
||||
}
|
||||
|
|
@ -158,12 +179,24 @@ class TranscodePipeline(BaseFilesPipeline):
|
|||
variants.append(
|
||||
self.media_variant(
|
||||
path=path,
|
||||
setting=setting,
|
||||
mimetype=setting["mimetype"],
|
||||
probe_result=probe_result,
|
||||
is_default=is_default,
|
||||
item=item,
|
||||
)
|
||||
)
|
||||
original_path = self.original_path(request.url)
|
||||
original_file = self.local_store_path(original_path)
|
||||
if original_file.exists():
|
||||
variants.append(
|
||||
self.media_variant(
|
||||
path=original_path,
|
||||
mimetype=self.original_mimetype(request.url),
|
||||
probe_result=media.probe_media(str(original_file)),
|
||||
is_default=False,
|
||||
item=item,
|
||||
)
|
||||
)
|
||||
return variants
|
||||
|
||||
def make_file_result(
|
||||
|
|
@ -201,6 +234,11 @@ class TranscodePipeline(BaseFilesPipeline):
|
|||
for _, _, path in self.variant_paths(request.url):
|
||||
if not cast(dict[str, Any] | None, self.store.stat_file(path, info)):
|
||||
return None
|
||||
if not cast(
|
||||
dict[str, Any] | None,
|
||||
self.store.stat_file(self.original_path(request.url), info),
|
||||
):
|
||||
return None
|
||||
self.inc_stats("uptodate")
|
||||
return self.make_file_result(
|
||||
request,
|
||||
|
|
@ -218,6 +256,23 @@ class TranscodePipeline(BaseFilesPipeline):
|
|||
tmp_file = f"{tmp_dir}/original"
|
||||
with open(tmp_file, "wb") as f:
|
||||
f.write(response.body)
|
||||
original_path = self.original_path(request.url)
|
||||
if not cast(
|
||||
dict[str, Any] | None,
|
||||
self.store.stat_file(original_path, info),
|
||||
):
|
||||
original_buf = read_asset(tmp_file)
|
||||
self.store.persist_file(
|
||||
original_path,
|
||||
original_buf,
|
||||
info,
|
||||
meta=self.get_media_meta(media.probe_media(tmp_file)),
|
||||
headers={
|
||||
"Content-Type": self.original_mimetype(
|
||||
request.url, response=response
|
||||
)
|
||||
},
|
||||
)
|
||||
for _, setting, final_path in self.variant_paths(request.url):
|
||||
stat = cast(
|
||||
dict[str, Any] | None,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue