30
30
from ivle import (util, studpath, interpret)
32
32
from ivle.database import User
33
from ivle.webapp.base.views import BaseView
34
from ivle.webapp.base.plugins import ViewPlugin
35
from ivle.webapp.errors import NotFound, Unauthorized, Forbidden
34
37
serveservice_path = os.path.join(ivle.conf.share_path, 'services/serveservice')
35
38
interpretservice_path = os.path.join(ivle.conf.share_path,
40
43
default_mimetype = "application/octet-stream"
41
44
zip_mimetype = "application/zip"
44
"""Handler for the Server application which serves pages."""
45
req.write_html_head_foot = False
47
# Get the username of the student whose work we are browsing, and the path
48
# on the local machine where the file is stored.
49
(login, path) = studpath.url_to_local(req.path)
51
owner = User.get_by_login(req.store, login)
54
req.throw_error(req.HTTP_NOT_FOUND,
55
"The path specified is invalid.")
57
serve_file(req, owner, path)
46
class ServeView(BaseView):
47
def __init__(self, req, path):
50
def authorize(self, req):
51
return req.user is not None
53
def render(self, req):
54
"""Handler for the Server application which serves pages."""
55
# Get the username of the student whose work we are browsing, and the
56
# path on the local machine where the file is stored.
57
(login, path) = studpath.url_to_local(self.path)
59
owner = User.get_by_login(req.store, login)
64
serve_file(req, owner, path)
59
66
def authorize(req):
60
67
"""Given a request, checks whether req.username is allowed to
61
access req.path. Returns None on authorization success. Raises
62
HTTP_FORBIDDEN on failure.
68
access req.path. Returns True on authorization success, False on failure.
70
# TODO: Reactivate public mode.
65
72
# Public mode authorization: any user can access any other user's
66
73
# files, BUT the accessed file needs to have its "ivle:published" flag
67
74
# turned on in the SVN status.
68
studpath.authorize_public(req)
70
# Private mode authorization: standard (only logged in user can access
71
# their own files, and can access all of them).
72
studpath.authorize(req)
75
# studpath.authorize_public(req)
77
# Private mode authorization: standard (only logged in user can access
78
# their own files, and can access all of them).
79
return studpath.authorize(req, req.user)
74
81
def serve_file(req, owner, filename, download=False):
75
82
"""Serves a file, using one of three possibilities: interpreting the file,
102
110
interpret.interpret_file(req, owner, user_jail_dir,
103
111
interpretservice_path, interp_object, gentle=True)
105
def serve_file_direct(req, filename, type):
106
"""Serves a file by directly writing it out to the response.
108
req: An IVLE request object.
109
filename: Filename in the local file system.
110
type: String. Mime type to serve the file with.
112
if not os.access(filename, os.R_OK):
113
req.throw_error(req.HTTP_NOT_FOUND,
114
"The specified file does not exist.")
115
req.content_type = type
116
req.sendfile(filename)
113
class Plugin(ViewPlugin):
115
('serve/*path', ServeView)