Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MediaType guessing #7326

Merged
merged 7 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions rerun_cpp/src/rerun/archetypes/asset3d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 1 addition & 26 deletions rerun_cpp/src/rerun/archetypes/asset3d_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ namespace rerun::archetypes {
return asset;
}


static std::optional<rerun::components::MediaType> guess_media_type(
const std::filesystem::path& path
);

// </CODEGEN_COPY_TO_HEADER>
#endif

Expand All @@ -61,27 +56,7 @@ namespace rerun::archetypes {

return Asset3D::from_bytes(
Collection<uint8_t>::take_ownership(std::move(data)),
Asset3D::guess_media_type(path)
rerun::components::MediaType::guess_from_path(path)
);
}

std::optional<rerun::components::MediaType> Asset3D::guess_media_type(
const std::filesystem::path& path
) {
std::filesystem::path file_path(path);
std::string ext = file_path.extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);

if (ext == ".glb") {
return rerun::components::MediaType::glb();
} else if (ext == ".gltf") {
return rerun::components::MediaType::gltf();
} else if (ext == ".obj") {
return rerun::components::MediaType::obj();
} else if (ext == ".stl") {
return rerun::components::MediaType::stl();
} else {
return std::nullopt;
}
}
} // namespace rerun::archetypes
4 changes: 0 additions & 4 deletions rerun_cpp/src/rerun/archetypes/asset_video.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 1 addition & 20 deletions rerun_cpp/src/rerun/archetypes/asset_video_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ namespace rerun::archetypes {
return asset;
}


static std::optional<rerun::components::MediaType> guess_media_type(
const std::filesystem::path& path
);

// </CODEGEN_COPY_TO_HEADER>
#endif

Expand All @@ -61,21 +56,7 @@ namespace rerun::archetypes {

return AssetVideo::from_bytes(
Collection<uint8_t>::take_ownership(std::move(data)),
AssetVideo::guess_media_type(path)
rerun::components::MediaType::guess_from_path(path)
);
}

std::optional<rerun::components::MediaType> AssetVideo::guess_media_type(
const std::filesystem::path& path
) {
std::filesystem::path file_path(path);
std::string ext = file_path.extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);

if (ext == ".mp4") {
return rerun::components::MediaType::mp4();
} else {
return std::nullopt;
}
}
} // namespace rerun::archetypes
4 changes: 0 additions & 4 deletions rerun_cpp/src/rerun/archetypes/encoded_image.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 4 additions & 21 deletions rerun_cpp/src/rerun/archetypes/encoded_image_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ namespace rerun::archetypes {
return image;
}

static std::optional<rerun::components::MediaType> guess_media_type(
const std::filesystem::path& path
);

// </CODEGEN_COPY_TO_HEADER>
#endif

Expand All @@ -64,22 +60,9 @@ namespace rerun::archetypes {
return Error(ErrorCode::FileRead, filepath.string());
}

return EncodedImage::from_bytes(file_bytes, EncodedImage::guess_media_type(filepath));
}

std::optional<rerun::components::MediaType> EncodedImage::guess_media_type(
const std::filesystem::path& path
) {
std::filesystem::path file_path(path);
std::string ext = file_path.extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);

if (ext == ".jpg" || ext == ".jpeg") {
return rerun::components::MediaType::jpeg();
} else if (ext == ".png") {
return rerun::components::MediaType::png();
} else {
return std::nullopt;
}
return EncodedImage::from_bytes(
file_bytes,
rerun::components::MediaType::guess_from_path(filepath)
);
}
} // namespace rerun::archetypes
3 changes: 3 additions & 0 deletions rerun_cpp/src/rerun/components/media_type.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 47 additions & 3 deletions rerun_cpp/src/rerun/components/media_type_ext.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
#include <algorithm>
#include <optional>
#include <string>
#include "media_type.hpp"

// Uncomment for better auto-complete while editing the extension.
// #define EDIT_EXTENSION

// It's undefined behavior to pre-declare std types, see http://www.gotw.ca/gotw/034.htm
// We want to use `std::filesystem::path`, so we have it include it in the header.
// <CODEGEN_COPY_TO_HEADER>

#include <filesystem>

// </CODEGEN_COPY_TO_HEADER>

namespace rerun {
namespace components {

Expand Down Expand Up @@ -86,9 +97,42 @@ namespace rerun {
return "video/mp4";
}

static std::optional<MediaType> guess_from_path(const std::filesystem::path& path);

// </CODEGEN_COPY_TO_HEADER>
}
};
#endif
} // namespace components
} // namespace rerun

std::optional<MediaType>
MediaType::guess_from_path(const std::filesystem::path& path) {
std::filesystem::path file_path(path);
std::string ext = file_path.extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);

// Images
if (ext == ".jpg" || ext == ".jpeg") {
return rerun::components::MediaType::jpeg();
} else if (ext == ".png") {
return rerun::components::MediaType::png();
}

// 3D Models
if (ext == ".glb") {
return MediaType::glb();
} else if (ext == ".gltf") {
return MediaType::gltf();
} else if (ext == ".obj") {
return MediaType::obj();
} else if (ext == ".stl") {
return MediaType::stl();
}

// Video
if (ext == ".mp4") {
return MediaType::mp4();
}

return std::nullopt;
}
}; // namespace components
}; // namespace rerun
18 changes: 1 addition & 17 deletions rerun_py/rerun_sdk/rerun/archetypes/asset3d_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,6 @@
from ..components import MediaType


def guess_media_type(path: str | pathlib.Path) -> MediaType | None:
from ..components import MediaType

ext = pathlib.Path(path).suffix.lower()
if ext == ".glb":
return MediaType.GLB
elif ext == ".gltf":
return MediaType.GLTF
elif ext == ".obj":
return MediaType.OBJ
elif ext == ".stl":
return MediaType.STL
else:
return None


class Asset3DExt:
"""Extension for [Asset3D][rerun.archetypes.Asset3D]."""

Expand Down Expand Up @@ -73,7 +57,7 @@ def __init__(
else:
blob = pathlib.Path(path).read_bytes()
if media_type is None:
media_type = guess_media_type(str(path))
media_type = MediaType.guess_from_path(path)

self.__attrs_init__(blob=blob, media_type=media_type)
return
Expand Down
12 changes: 1 addition & 11 deletions rerun_py/rerun_sdk/rerun/archetypes/asset_video_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@
from ..components import MediaType


def guess_media_type(path: str | pathlib.Path) -> MediaType | None:
from ..components import MediaType

ext = pathlib.Path(path).suffix.lower()
if ext == ".mp4":
return MediaType.MP4
else:
return None


class AssetVideoExt:
"""Extension for [AssetVideo][rerun.archetypes.AssetVideo]."""

Expand Down Expand Up @@ -64,7 +54,7 @@ def __init__(
else:
blob = pathlib.Path(path).read_bytes()
if media_type is None:
media_type = guess_media_type(str(path))
media_type = MediaType.guess_from_path(path)

self.__attrs_init__(blob=blob, media_type=media_type)
return
Expand Down
14 changes: 1 addition & 13 deletions rerun_py/rerun_sdk/rerun/archetypes/encoded_image_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@
]


def guess_media_type(path: str | pathlib.Path) -> MediaType | None:
from ..components import MediaType

ext = pathlib.Path(path).suffix.lower()
if ext == ".jpg" or ext == ".jpeg":
return MediaType.JPEG
elif ext == ".png":
return MediaType.PNG
else:
return None


class EncodedImageExt:
"""Extension for [EncodedImage][rerun.archetypes.EncodedImage]."""

Expand Down Expand Up @@ -100,7 +88,7 @@ def __init__(
blob = pathlib.Path(path).read_bytes()

if media_type is None:
media_type = guess_media_type(str(path))
media_type = MediaType.guess_from_path(path)

self.__attrs_init__(blob=blob, media_type=media_type, draw_order=draw_order, opacity=opacity)
return
Expand Down
29 changes: 29 additions & 0 deletions rerun_py/rerun_sdk/rerun/components/media_type_ext.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
Expand Down Expand Up @@ -91,3 +92,31 @@ def deferred_patch_class(cls: Any) -> None:
cls.STL = cls("model/stl")

cls.MP4 = cls("video/mp4")

@staticmethod
def guess_from_path(path: str | Path) -> MediaType | None:
from ..components import MediaType

ext = Path(path).suffix.lower()

# Images
if ext == ".jpg" or ext == ".jpeg":
return MediaType.JPEG
elif ext == ".png":
return MediaType.PNG

# 3D Models
if ext == ".glb":
return MediaType.GLB
elif ext == ".gltf":
return MediaType.GLTF
elif ext == ".obj":
return MediaType.OBJ
elif ext == ".stl":
return MediaType.STL

# Video
if ext == ".mp4":
return MediaType.MP4

return None
Loading