the metadata fetcher
[bse.git] / site / cgi-bin / modules / BSE / TB / MetaEntry.pm
CommitLineData
f5b7b326 1package BSE::TB::MetaEntry;
6430ee52
TC
2use strict;
3use base 'Squirrel::Row';
4
b242c5a6 5our $VERSION = "1.004";
cb7fd78d 6
6430ee52
TC
7sub table {
8 "bse_article_file_meta";
9}
10
11sub columns {
f5b7b326 12 qw/id file_id name content_type value appdata owner_type/;
6430ee52
TC
13}
14
15sub defaults {
16 content_type => "text/plain",
c840f7f9 17 appdata => 1,
6430ee52
TC
18}
19
36e373a9
TC
20sub is_text {
21 $_[0]->content_type eq "text/plain"
22}
23
c29434c9
TC
24sub is_text_type {
25 $_[0]->content_type =~ m(^text/);
26}
27
28sub value_text {
29 my ($self) = @_;
30
31 $self->is_text_type or return;
32
33 my $value = $self->value;
34 utf8::decode($value) or return;
35
36 return $value;
37}
38
39sub set_value_text {
40 my ($self, $value) = @_;
41
42 $self->is_text_type or return;
43
44 utf8::encode($value);
45
46 $self->set_value($value);
47
48 1;
49}
50
b242c5a6
TC
51sub retrieve_json {
52 my ($self) = @_;
53
54 return unless $self->content_type
55 =~ m!\A(?:application/json|text/x-json(?: encoding=(?:"utf-8"|utf-8))?)\z!;
56
57 require JSON;
58 my $json = JSON->new->utf8;
59 my $data;
60 return unless eval { $data = $json->decode($self->value); 1 };
61 return $data;
62}
63
6430ee52 641;