Use centralised pub/sub
[gruntmaster-handlers.git] / lib / Gruntmaster / Handlers.pm
CommitLineData
9f02ff25
MG
1package Gruntmaster::Handlers;
2
3use 5.014000;
4use strict;
5use warnings;
6our $VERSION = '0.001';
7
9f02ff25
MG
8use Apache2::Access;
9use Apache2::AuthzCaps qw/hascaps/;
10use Apache2::RequestRec;
11use Apache2::RequestIO;
12use Apache2::Request;
13use Apache2::Const qw/OK DECLINED/;
14use Apache2::Log;
15use Apache2::Upload;
16
17use Cwd qw/cwd/;
18use File::Basename qw/fileparse/;
19use File::Temp qw/tempdir/;
20use File::Copy qw/move/;
b16e6cc2 21use Gruntmaster::Data qw/contest_start contest_end push_job set_job_inmeta PUBLISH/;
5855b3bd 22use Gruntmaster::Page::Log;
9f02ff25
MG
23
24sub submit{
25 my $r = shift;
26 chdir $r->dir_config('root');
27 my $req = Apache2::Request->new($r);
5855b3bd
MG
28 my ($problem, $format, $contest, $private) = map {scalar $req->param($_)} 'problem', 'prog_format', 'contest', 'private';
29 my $prog;
30 $req->upload('prog')->slurp($prog);
31 die if defined $contest && $contest !~ /^\w+$/ ;
32 die if defined $contest && (time < contest_start $contest || time > contest_end $contest);
33
34 my $job = push_job (
35 date => time,
36 problem => $problem,
37 user => $r->user,
38 defined $private ? (private => $private) : (),
39 defined $contest ? (contest => $contest, private => 1) : (),
40 filesize => length $prog,
41 extension => Gruntmaster::Page::Log::FORMAT_EXTENSION->{$format},
42 );
43
44 set_job_inmeta $job, {
45 files => {
46 prog => {
47 format => $format,
48 name => 'prog.' . Gruntmaster::Page::Log::FORMAT_EXTENSION->{$format},
49 content => $prog,
50 }
9f02ff25 51 }
9f02ff25
MG
52 };
53
b16e6cc2 54 PUBLISH 'jobs', $job;
9f02ff25
MG
55 $r->print("Job submitted");
56 OK
57}
58
5855b3bd
MG
59=begin comment
60
9f02ff25
MG
61sub private{
62 my $r = shift;
63 my $dir = (fileparse $r->uri)[1];
64 my $user = $r->user;
65 chdir $r->dir_config('root') . $dir;
66
67 for my $requirement (map { $_->{requirement} } @{$r->requires}) {
68 my ($command, @args) = split ' ', $requirement;
69
70 given ($command){
71 when('admin-if-private'){
72 my $meta = LoadFile 'meta.yml';
73 return OK if !$meta->{private} || ($r->user && hascaps $r->user, 'gmadm')
74 }
75
76 }
77 }
78
79 DECLINED
80}
81
5855b3bd
MG
82=end comment
83
84=cut
85
9f02ff25
MG
861;
87__END__
88# Below is stub documentation for your module. You'd better edit it!
89
90=head1 NAME
91
92Gruntmaster::Handlers - Perl extension for blah blah blah
93
94=head1 SYNOPSIS
95
96 use Gruntmaster::Handlers;
97 blah blah blah
98
99=head1 DESCRIPTION
100
101Stub documentation for Gruntmaster::Handlers, created by h2xs. It looks like the
102author of the extension was negligent enough to leave the stub
103unedited.
104
105Blah blah blah.
106
107
108=head1 SEE ALSO
109
110Mention other useful documentation such as the documentation of
111related modules or operating system documentation (such as man pages
112in UNIX), or any relevant external documentation such as RFCs or
113standards.
114
115If you have a mailing list set up for your module, mention it here.
116
117If you have a web site set up for your module, mention it here.
118
119=head1 AUTHOR
120
121Marius Gavrilescu, E<lt>marius@E<gt>
122
123=head1 COPYRIGHT AND LICENSE
124
125Copyright (C) 2013 by Marius Gavrilescu
126
127This library is free software; you can redistribute it and/or modify
128it under the same terms as Perl itself, either Perl version 5.18.1 or,
129at your option, any later version of Perl 5 you may have available.
130
131
132=cut
This page took 0.018554 seconds and 4 git commands to generate.