-
Notifications
You must be signed in to change notification settings - Fork 7
/
Pack.pm
133 lines (103 loc) · 3.1 KB
/
Pack.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package SixteenColors::Schema::ResultSet::Pack;
use strict;
use warnings;
use SixteenColors::Archive;
use Image::TextMode::SAUCE;
use Try::Tiny;
use XML::Atom::SimpleFeed;
use base 'DBIx::Class::ResultSet::Data::Pageset';
sub new_from_file {
my ( $self, $file, $year, $c ) = @_;
my ( $warn, $archive );
eval {
local $SIG{ __WARN__ } = sub { $warn = shift };
$archive = SixteenColors::Archive->new( { file => $file } );
};
if ( $warn or $@ ) {
warn sprintf "Problem indexing pack '%s': %s", $file, $warn || $@;
return;
}
my @manifest = $archive->files;
my $schema = $self->result_source->schema;
$schema->txn_begin;
my $pack;
my $pack_file = $c->path_to( 'root',
$self->result_class->pack_file_location( $file, $year ) );
try {
$pack_file->dir->mkpath;
File::Copy::copy( $file, "${pack_file}" ) unless -e "${pack_file}";
$pack
= $self->create( { file_path => "${pack_file}", year => $year } );
my $dir = $pack->extract;
for my $f ( @manifest ) {
next unless my $name = $dir->exists( $f );
next if -d $name;
my $sauce = Image::TextMode::SAUCE->new;
my $fh = $name->open( 'r' );
$sauce->read( $fh );
close( $fh );
my $newfile = $pack->add_to_files(
{ file_path => $f,
( $sauce->has_sauce ? ( sauce => $sauce ) : () )
}
);
next unless $newfile->is_textmode;
$newfile->fulltext(
Image::TextMode::Loader->load( "$name" )->as_ascii );
}
}
catch {
unlink $pack_file;
$schema->txn_rollback;
die $_;
};
$schema->txn_commit;
return $pack;
}
sub random {
my $self = shift;
$self->search( {}, { order_by => 'RANDOM()' } );
}
sub recent {
my ( $self ) = @_;
return $self->search( {}, { order_by => 'ctime DESC' } );
}
sub TO_JSON {
my $self = shift;
$self = $self->search( {}, {
prefetch => {
group_joins => 'art_group'
}
} );
return [ map {
{ name => $_->canonical_name,
filename => $_->filename,
year => $_->year,
month => $_->month,
groups => [
map { { name => $_->name, shortname => $_->shortname } }
map { $_->art_group }
$_->group_joins
],
}
} $self->all ];
}
sub TO_FEED {
my( $self, $c, $defaults ) = @_;
$self = $self->search( {}, { order_by => 'ctime DESC' } );
my %feed_info = %$defaults;
$feed_info{ updated } = $self->first->ctime . 'Z';
my $feed = XML::Atom::SimpleFeed->new( %feed_info );
$self->reset;
while( my $pack = $self->next ) {
my $link = $c->uri_for( '/pack', $pack->canonical_name );
$feed->add_entry(
link => $link,
id => $link,
title => $pack->canonical_name,
summary => $pack->description_as_html || undef,
);
}
return $feed;
}
1;