Move filesystem access to lib/
authorVincent Tondellier <tonton+hg@team1664.org>
Mon, 28 May 2012 00:24:07 +0200
changeset 11 0bef3b8087c1
parent 10 4668386ec082
child 12 c98d3fa4a948
Move filesystem access to lib/
CrashTest.pl
lib/CrashTest/Storage/FileSystem.pm
--- a/CrashTest.pl	Mon May 28 00:23:21 2012 +0200
+++ b/CrashTest.pl	Mon May 28 00:24:07 2012 +0200
@@ -4,10 +4,12 @@
 use UUID;
 use Mojo::JSON;
 use Mojo::UserAgent;
+use lib 'lib';
+use CrashTest::Storage::FileSystem;
 
 my @valid_params = qw/Add-ons Distributor ProductName ReleaseChannel StartupTime UserID Version BuildID CrashTime Comments/;
 my $config = plugin 'Config';
-my $data_path = $config->{DataDir};
+my $storage = CrashTest::Storage::FileSystem->new($config->{DataDir});
 
 
 helper scm_file_link => sub {
@@ -22,7 +24,8 @@
         my $scmrepo = "$scm:$repo";
         if(exists($config->{ScmLinks}->{$scmrepo})) {
             return Mojo::ByteStream->new($self->link_to("$filename:$line" =>
-                    $self->render(inline => $config->{ScmLinks}->{$scmrepo}, repo => $repo, scmpath => $scmpath, rev => $rev, line => $line, partial => 1)
+                    $self->render(inline => $config->{ScmLinks}->{$scmrepo},
+                        repo => $repo, scmpath => $scmpath, rev => $rev, line => $line, partial => 1)
                 ));
         }
         #return $file;
@@ -53,47 +56,13 @@
 # Upload form in DATA section
 get '/' => sub {
     my $self = shift;
-
-    my @files;
-    opendir my ($dh), $data_path or die $!;
-    my @allfiles = readdir $dh;
-    foreach(@allfiles)
-    {
-        if($_ =~ /(.*)\.json$/)
-        {
-            my $filename = File::Spec->catfile($data_path, $_);
-            push @files, {
-                file => $filename,
-                uuid => $1,
-                signature => $1,
-                product => "",
-                date => (stat $filename)[9],
-            };
-        }
-    }
-    closedir $dh;
-
-    my @sorted_files = ( sort { $b->{date} <=> $a->{date} } @files );
-    @sorted_files = @sorted_files[0..19] if scalar(@sorted_files) > 20;
-
-    $self->stash(files => \@sorted_files);
-
+    $self->stash(files => $storage->index());
     $self->render('index');
 } => 'index';
 
 get '/report/:uuid' => [ uuid => qr/[0-9a-fA-F-]+/ ] => sub {
     my $self = shift;
-
-    open JSON, '<', File::Spec->catfile($data_path, $self->param('uuid') . '.json') or die $!;
-    my @json_content_lines = <JSON>;
-    my $json_content = join('', @json_content_lines);
-    close JSON;
-
-    my $json = Mojo::JSON->new;
-    my $processed_data = $json->decode($json_content);
-
-    $self->stash(processed_data => $processed_data);
-
+    $self->stash(processed_data => $storage->get_processed_data($self->param('uuid')));
     $self->render('report/crash');
 } => 'report';
 
@@ -105,22 +74,21 @@
     my ($uuid, $uuidstr);
     UUID::generate($uuid);
     UUID::unparse($uuid, $uuidstr);
-    my $dmp_file = File::Spec->catfile($data_path, "$uuidstr.dmp");
+    my $dmp_file = "/tmp/$uuidstr.dmp";
     $file->move_to($dmp_file);
 
-    # Create json for the params
-    my %paramshash = map { $_ => $self->req->param($_) } $self->req->param;
-
     my $out = qx($config->{MinidumpStackwalkJSON} "$dmp_file" $config->{SymbolsPath} 2>/dev/null) or die $!;
 
     my $json = Mojo::JSON->new;
     my $pjson = $json->decode($out);
+
+    # Create json for the params
+    my %paramshash = map { $_ => $self->req->param($_) } $self->req->param;
     $pjson->{client_info} = \%paramshash;
 
-    my $j = $json->encode($pjson);
-    open JSON, '>', File::Spec->catfile($data_path, "$uuidstr.json") or die $!;
-    print JSON $j;
-    close JSON;
+    $storage->store_dump($uuidstr, $file);
+    $storage->store_processed_data($uuidstr, $pjson);
+    unlink $dmp_file if -w $dmp_file;
 
     # reply
     $self->render_text($pjson->{status});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/CrashTest/Storage/FileSystem.pm	Mon May 28 00:24:07 2012 +0200
@@ -0,0 +1,72 @@
+package CrashTest::Storage::FileSystem;
+
+use strict;
+use warnings;
+
+sub new {
+    my ($class, $data_path) = @_;
+    my $self  = {};
+    $self->{data_path} = $data_path;
+    bless ($self, $class);
+    return $self;
+}
+
+sub index {
+    my ($self) = @_;
+    my @files;
+    opendir my ($dh), $self->{data_path} or die $!;
+    my @allfiles = readdir $dh;
+    foreach(@allfiles)
+    {
+        if($_ =~ /(.*)\.json$/)
+        {
+            my $filename = File::Spec->catfile($self->{data_path}, $_);
+            push @files, {
+                file => $filename,
+                uuid => $1,
+                signature => $1,
+                product => "",
+                date => (stat $filename)[9],
+            };
+        }
+    }
+    closedir $dh;
+
+    my @sorted_files = ( sort { $b->{date} <=> $a->{date} } @files );
+    @sorted_files = @sorted_files[0..19] if scalar(@sorted_files) > 20;
+
+    return \@sorted_files;
+}
+
+sub get_processed_data {
+    my ($self, $uuid) = @_;
+
+    open JSON, '<', File::Spec->catfile($self->{data_path}, $uuid . '.json') or die $!;
+    my @json_content_lines = <JSON>;
+    my $json_content = join('', @json_content_lines);
+    close JSON;
+
+    my $json = Mojo::JSON->new;
+    my $processed_data = $json->decode($json_content);
+
+    return $processed_data;
+}
+
+sub store_processed_data {
+    my ($self, $uuid, $pjson) = @_;
+
+    my $json = Mojo::JSON->new;
+    my $j = $json->encode($pjson);
+    open JSON, '>', File::Spec->catfile($self->{data_path}, "$uuid.json") or die $!;
+    print JSON $j;
+    close JSON;
+}
+
+sub store_dump {
+    my ($self, $uuid, $file) = @_;
+
+    my $dmp_file = File::Spec->catfile($self->{data_path}, "$uuid.dmp");
+    $file->move_to($dmp_file);
+}
+
+1;