NAME

Net::Nostr::MediaAttachment - NIP-92 Media Attachments

SYNOPSIS

use Net::Nostr::MediaAttachment;

# Build an imeta tag
my $tag = Net::Nostr::MediaAttachment->imeta_tag(
    url      => 'https://example.com/photo.jpg',
    m        => 'image/jpeg',
    dim      => '1920x1080',
    alt      => 'A scenic photo',
    blurhash => 'eVF$^OI',
    fallback => ['https://alt.example.com/photo.jpg'],
);

# Attach to an event
my $event = Net::Nostr::Event->new(
    pubkey  => $pubkey,
    kind    => 1,
    content => 'Check this out https://example.com/photo.jpg',
    tags    => [$tag],
);

# Parse imeta tags from an event
my @attachments = Net::Nostr::MediaAttachment->from_event($event);
for my $att (@attachments) {
    say $att->url;
    say $att->m;        # MIME type
    say $att->dim;      # e.g. '1920x1080'
    say $att->alt;      # accessibility description
}

# Get metadata for a specific URL
my $info = Net::Nostr::MediaAttachment->for_url($event, $url);

CONSTRUCTOR

new

my $att = Net::Nostr::MediaAttachment->new(
    url      => 'https://example.com/photo.jpg',
    m        => 'image/jpeg',
    dim      => '1920x1080',
    fallback => ['https://alt.example.com/photo.jpg'],
);

Creates a new media attachment object. All fields are optional. fallback defaults to [] and fields defaults to {}. Croaks on unknown arguments.

DESCRIPTION

Implements NIP-92 (Media Attachments). Provides methods to build imeta tags, parse them from events, and look up metadata by URL.

Each imeta tag is variadic with space-delimited key/value entries. It MUST have a url and at least one other field. Fields from NIP-94 are supported: m, x, ox, size, dim, magnet, i, blurhash, thumb, image, summary, alt, service, and fallback (which may appear multiple times).

imeta_tag

my $tag = Net::Nostr::MediaAttachment->imeta_tag(
    url      => 'https://example.com/photo.jpg',
    m        => 'image/jpeg',
    dim      => '1920x1080',
    alt      => 'Description',
    x        => $sha256_hex,
    fallback => ['https://alt1.com/photo.jpg'],
);

Creates an imeta tag arrayref. url is required, and at least one other field must be provided. fallback accepts an arrayref of URLs.

from_tag

my $info = Net::Nostr::MediaAttachment->from_tag($imeta_tag);
say $info->url;
say $info->m;

Parses a single imeta tag arrayref into a Net::Nostr::MediaAttachment object. Unknown fields are available via the fields accessor hashref.

from_event

my @attachments = Net::Nostr::MediaAttachment->from_event($event);

Returns a list of Net::Nostr::MediaAttachment objects, one for each imeta tag in the event.

for_url

my $info = Net::Nostr::MediaAttachment->for_url($event, $url);

Returns the Net::Nostr::MediaAttachment for the given URL, or undef if not found.

ACCESSORS

url

Media URL.

m

MIME type (e.g. 'image/jpeg').

x

SHA-256 hash of the file.

ox

SHA-256 hash of the original file before any transformations.

size

File size in bytes.

dim

Dimensions string (e.g. '1920x1080').

magnet

Magnet URI.

i

Torrent infohash.

blurhash

Blurhash string.

thumb

Thumbnail URL.

image

Image URL.

summary

Description or summary.

alt

Accessibility description.

service

Service URL.

fallback

Arrayref of fallback URLs.

fields

Hashref of unknown/extension fields.

SEE ALSO

NIP-92, NIP-94, Net::Nostr, Net::Nostr::Event