lib/CrashTest/Model/CrashProcessor.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Sat, 12 Dec 2015 15:55:55 +0100
changeset 79 4ae8bb6f8a96
parent 78 0ebef32c34af
child 122 8692800ec9ba
permissions -rw-r--r--
Small config reorganization

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package CrashTest::Model::CrashProcessor;
use Mojo::Base -base;
use File::Temp;
use UUID;

has [ qw/app config/ ];
has processors => sub { return [] };

sub _register_instance {
    my ($self, $processor_instance) = @_;

    push @{$self->processors}, $processor_instance;
}

sub load_plugins {
    my ($self) = @_;

    my @conf_processors = keys %{$self->config->{Processor}->{CrashProcessor}};
    for my $module (@conf_processors) {
        $self->app->plugin('CrashTest::Plugin::CrashProcessor::' . $module, {
                config => $self->config,
                cb => sub { my $i = shift; $self->_register_instance($i); }
        });
    }
}

sub find_processor {
    my ($self, $req) = @_;

    # find the first processor that accepts this kind of crash
    foreach my $processor(@{$self->processors}) {
        my $validator = $processor->validate($req);
        if(!$validator->has_error) {
            return $processor->task_name;
        }
    }
    return undef;
}

sub decode {
    my ($self, $task_name, $req) = @_;

    my $tmpdir = $self->config->{Processor}->{TmpDir};

    my $files;
    foreach my $up_name(map { $_->name } @{$req->uploads}) {
        foreach my $up(@{$req->every_upload($up_name)}) {
            my $fh = File::Temp->new(DIR => $tmpdir, SUFFIX => '.dat');
            $fh->unlink_on_destroy(0);

            #print $fh $up->slurp;
            $up->move_to($fh->filename);

            $files->{$up_name} ||= [];
            push @{$files->{$up_name}}, $fh->filename;
        }
    }

    my ($uuid, $uuidstr);
    UUID::generate($uuid);
    UUID::unparse($uuid, $uuidstr);
    my $id = $self->app->minion->enqueue($task_name => [ $uuidstr, $req->params->to_hash, $files ]);
}

1;