Annotation of loncom/homework/daxeopen.pm, revision 1.2
1.1 damieng 1: # The LearningOnline Network
2: # Opening converted problems and directory listings for Daxe
3: #
1.2 ! damieng 4: # $Id: daxeopen.pm,v 1.1 2015/12/03 20:40:27 damieng Exp $
1.1 damieng 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ###
29:
30: package Apache::daxeopen;
31:
32: use Apache::Constants;
33: use DateTime;
34: use Try::Tiny;
35: use File::stat;
36: use Fcntl ':mode';
37:
38: use Apache::loncommon;
39: use Apache::lonnet;
40: use Apache::pre_xml;
41: use Apache::html_to_xml;
42: use Apache::post_xml;
43:
44:
45: sub handler {
46: my $request = shift;
47: my $uri = $request->uri;
48: $uri =~ s/^\/daxeopen//;
49: &Apache::loncommon::no_cache($request);
50: if ($uri =~ /\/$/) {
51: return directory_listing($uri, $request);
52: } elsif ($uri =~ /\.(task|problem|exam|quiz|assess|survey|library)$/) {
53: return convert_problem($uri, $request);
54: } else {
55: # Apache should send other files directly
1.2 ! damieng 56: $request->status(406);
! 57: return OK;
1.1 damieng 58: }
59: }
60:
61: sub convert_problem {
62: my ($uri, $request) = @_;
63:
64: my $file = &Apache::lonnet::filelocation('', $uri);
65: &Apache::lonnet::repcopy($file);
66: if (! -e $file) {
1.2 ! damieng 67: $request->status(404);
! 68: return OK;
1.1 damieng 69: }
70: try {
71: my $warnings = 0; # no warning printed
72: my $textref = &Apache::pre_xml::pre_xml($file, $warnings);
73: $textref = &Apache::html_to_xml::html_to_xml($textref, $warnings);
74: my $text = &Apache::post_xml::post_xml($textref, $file, $warnings);
75: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
76: $request->print($text);
77: return OK;
78: } catch {
1.2 ! damieng 79: $request->content_type('text/plain');
! 80: $request->print("convert failed for $file: $_");
! 81: $request->status(406);
! 82: return OK;
1.1 damieng 83: };
84: }
85:
86: sub directory_listing {
87: my ($uri, $request) = @_;
88: my $dirpath = &Apache::lonnet::filelocation('', $uri);
89: if (! -e $dirpath) {
1.2 ! damieng 90: $request->status(404);
! 91: return OK;
1.1 damieng 92: }
93: $dirpath =~ s/\/$//;
94: opendir my $dir, $dirpath or die "Cannot open directory: $dirpath";
95: my @files = readdir $dir;
96: closedir $dir;
97: my $res = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
98: my $dirname = $dirpath;
99: $dirname =~ s/^.*\/([^\/]*)$/$1/;
100: $res .= "<directory name=\"$dirname\">\n";
101: foreach my $name (@files) {
102: if ($name eq '.' || $name eq '..') {
103: next;
104: }
105: if ($name =~ /\.(bak|log|meta|save)$/) {
106: next;
107: }
108: $sb = stat($dirpath.'/'.$name);
109: my $mode = $sb->mode;
110: if (S_ISDIR($mode)) {
111: $res .= "<directory name=\"$name\"/>\n";
112: } else {
113: $res .= "<file name=\"$name\"";
114: my $size = $sb->size; # total size of file, in bytes
115: $res .= " size=\"$size\"";
116: my $mtime = $sb->mtime; # last modify time in seconds since the epoch
117: my $dt = DateTime->from_epoch(epoch => $mtime);
118: my $modified = $dt->iso8601().'Z';
119: $res .= " modified=\"$modified\"";
120: $res .= "/>\n";
121: }
122: }
123: $res .= "</directory>\n";
124: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
125: $request->print($res);
126: return OK;
127: }
128:
129: 1;
130: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>