~azzar1/unity/add-show-desktop-key

138 by mattgiuca
Added new app: fileservice. (The Ajax service behind the file browser).
1
# IVLE
2
# Copyright (C) 2007-2008 The University of Melbourne
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
# App: File Service (AJAX server)
19
# Author: Matt Giuca
20
# Date: 9/1/2008
21
22
# This application is an AJAX service. Receives file handling instructions as
23
# requests. Performs actions on the student's workspace, and returns directory
24
# listings in JSON.
25
140 by mattgiuca
doc/dependencies: Added dependency on pysvn.
26
# This rather large documentation explains the request and response to the
27
# file service app (it should probably be taken to a separate document).
28
138 by mattgiuca
Added new app: fileservice. (The Ajax service behind the file browser).
29
# This is not intended to be accessed directly by the user. It is targeted by
30
# AJAX calls in applications such as browser and editor.
31
140 by mattgiuca
doc/dependencies: Added dependency on pysvn.
32
# Application usage: The input to the application is determined by the fields
33
# passed in as HTTP variables (either in the URL or message body). Also, in
34
# keeping with REST, actions only take effect if this is a POST request as
35
# opposed to a GET request (although a GET request is still allowed to just
36
# get a listing or file dump). Also, the "path" (the part of the URL
37
# after "fileservice" and before the GET variables) is taken into account.
38
39
# Aside from the side-effects to the server (note: side-effects are only
40
# possible for POST requests), the response takes two parts. The response
41
# header contains information about success or failure of the operation. The
42
# response body may contain the requested file.
43
44
# Fileservice has two separate roles: First, an action is performed. This may
45
# be a copy, write, or svn up operation. Then, a file or directory listing is
46
# returned. This directory listing may be completely separate from the action,
47
# but they are performed together because the client will usually want to
48
# perform some action, then update its display as a result of the action.
49
50
# GET requests will have all variables ignored, and the only behaviour will be
51
# to generate the directory or file listing. POST requests will result in an
52
# action if one is specified. If the action is UNSUCCESSFUL, returns the
53
# header "X-IVLE-Action-Error: <errormessage>". Successful actions succeed
54
# silently. Note that the action does not affect the HTTP response code (it
55
# may be 200 even upon failure).
56
57
# The path (req.path) controls which file or directory will be
58
# returned. If it is a file, returns the header "X-IVLE-Return: File" and
59
# status 200 OK. The response body is a verbatim dump of the file specified.
60
# The Content-Type will probably be text/plain but should not be relied upon.
61
# If it is a directory, returns the header "X-IVLE-Return: Dir" and status
62
# 200 OK. The response body is a JSON directory listing (see below). The
63
# Content-Type cannot be relied upon. If the file is not found or there is
64
# some other read error, returns no X-IVLE-Return header, a 400-level
65
# response status. (404 File Not Found, 403 Forbidden, etc), and a header
143 by mattgiuca
fileservice: Completely wrote the "return" function. Now sends files and
66
# "X-IVLE-Return-Error: <errormessage>".
140 by mattgiuca
doc/dependencies: Added dependency on pysvn.
67
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
68
# See action.py for a full description of the actions.
69
# See listing.py for a full description of the output format of the directory
70
# listing.
140 by mattgiuca
doc/dependencies: Added dependency on pysvn.
71
143 by mattgiuca
fileservice: Completely wrote the "return" function. Now sends files and
72
import os
153 by mattgiuca
fileservice: remove now works on directories too (recurses).
73
import shutil
146 by mattgiuca
fileservice: Dir listing, fixed fatal error if a subversioned file is missing.
74
import stat
143 by mattgiuca
fileservice: Completely wrote the "return" function. Now sends files and
75
import time
147 by mattgiuca
conf: Moved "default_mimetype" configuration constant from conf/app/server.py
76
import mimetypes
138 by mattgiuca
Added new app: fileservice. (The Ajax service behind the file browser).
77
140 by mattgiuca
doc/dependencies: Added dependency on pysvn.
78
import cjson
141 by mattgiuca
fileservice: Added scaffolding for this module (2 functions, and code to call
79
import pysvn
140 by mattgiuca
doc/dependencies: Added dependency on pysvn.
80
143 by mattgiuca
fileservice: Completely wrote the "return" function. Now sends files and
81
from common import (util, studpath)
147 by mattgiuca
conf: Moved "default_mimetype" configuration constant from conf/app/server.py
82
import conf.mimetypes
143 by mattgiuca
fileservice: Completely wrote the "return" function. Now sends files and
83
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
84
import action, listing
140 by mattgiuca
doc/dependencies: Added dependency on pysvn.
85
143 by mattgiuca
fileservice: Completely wrote the "return" function. Now sends files and
86
# Mime types
87
# application/json is the "best" content type but is not good for
88
# debugging because Firefox just tries to download it
152 by mattgiuca
fileservice: Improved the action handler:
89
mime_dirlisting = "text/html"
143 by mattgiuca
fileservice: Completely wrote the "return" function. Now sends files and
90
#mime_dirlisting = "application/json"
91
138 by mattgiuca
Added new app: fileservice. (The Ajax service behind the file browser).
92
def handle(req):
140 by mattgiuca
doc/dependencies: Added dependency on pysvn.
93
    """Handler for the File Services application."""
262 by mattgiuca
studpath: Added "authorize" function which checks the logged in user against
94
    # Make sure the logged in user has permission to see this file
95
    # FIXME: Still need to authorize subpaths in actions
96
    studpath.authorize(req)
138 by mattgiuca
Added new app: fileservice. (The Ajax service behind the file browser).
97
98
    # Set request attributes
99
    req.write_html_head_foot = False     # No HTML
100
141 by mattgiuca
fileservice: Added scaffolding for this module (2 functions, and code to call
101
    # Get all the arguments, if POST.
102
    # Ignore arguments if not POST, since we aren't allowed to cause
103
    # side-effects on the server.
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
104
    act = None
141 by mattgiuca
fileservice: Added scaffolding for this module (2 functions, and code to call
105
    fields = None
157 by mattgiuca
Fileservice: Added action "putfile".
106
    if req.method == 'POST':
141 by mattgiuca
fileservice: Added scaffolding for this module (2 functions, and code to call
107
        fields = req.get_fieldstorage()
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
108
        act = fields.getfirst('action')
141 by mattgiuca
fileservice: Added scaffolding for this module (2 functions, and code to call
109
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
110
    if act is not None:
152 by mattgiuca
fileservice: Improved the action handler:
111
        try:
166 by mattgiuca
fileservice:
112
            action.handle_action(req, act, fields)
159 by mattgiuca
fileservice: Split growing module into three modules: top-level,
113
        except action.ActionError, message:
152 by mattgiuca
fileservice: Improved the action handler:
114
            req.headers_out['X-IVLE-Action-Error'] = str(message)
141 by mattgiuca
fileservice: Added scaffolding for this module (2 functions, and code to call
115
166 by mattgiuca
fileservice:
116
    listing.handle_return(req)