Package flumotion :: Package component :: Package misc :: Package httpserver :: Module localpath
[hide private]

Source Code for Module flumotion.component.misc.httpserver.localpath

 1  # -*- Mode: Python; test-case-name: flumotion.test.test_component_providers -*- 
 2  # vi:si:et:sw=4:sts=4:ts=4 
 3  # 
 4  # Flumotion - a streaming media server 
 5  # Copyright (C) 2004,2005,2006,2007,2008 Fluendo, S.L. (www.fluendo.com). 
 6  # All rights reserved. 
 7   
 8  # This file may be distributed and/or modified under the terms of 
 9  # the GNU General Public License version 2 as published by 
10  # the Free Software Foundation. 
11  # This file is distributed without any warranty; without even the implied 
12  # warranty of merchantability or fitness for a particular purpose. 
13  # See "LICENSE.GPL" in the source distribution for more information. 
14   
15  # Licensees having purchased or holding a valid Flumotion Advanced 
16  # Streaming Server license may use this file in accordance with the 
17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
18  # See "LICENSE.Flumotion" in the source distribution for more information. 
19   
20  # Headers in this file shall remain intact. 
21   
22  import os 
23   
24  from twisted.web import static 
25   
26  from flumotion.component.misc.httpserver import fileprovider 
27  from flumotion.component.misc.httpserver.fileprovider import InsecureError 
28  from flumotion.component.misc.httpserver.fileprovider import NotFoundError 
29   
30   
31 -def loadMimeTypes():
32 # Add our own mime types to the ones parsed from /etc/mime.types 33 d = static.loadMimeTypes() 34 d['.flv'] = 'video/x-flv' 35 d['.mp4'] = 'video/mp4' 36 return d
37 38
39 -class LocalPath(fileprovider.FilePath):
40 41 contentTypes = loadMimeTypes() 42 43 # Override parent class property by an attribute 44 mimeType = None 45
46 - def __init__(self, path):
47 self.path = path 48 ext = os.path.splitext(path)[1] 49 self.mimeType = self.contentTypes.get(ext.lower(), None)
50
51 - def __str__(self):
52 return "<LocalPath '%s'>" % self.path
53
54 - def child(self, name):
55 childpath = self._getChildPath(name) 56 return type(self)(childpath)
57
58 - def open(self):
59 raise NotImplementedError()
60 61 62 ## Protected Methods ## 63
64 - def _getChildPath(self, name):
65 """ 66 @param name: the name of a child of the pointed directory 67 @type name: str 68 69 @return: the path of the child 70 @rtype: str 71 @raises InsecureError: if the specified name compromise security 72 """ 73 norm = os.path.normpath(name) 74 if os.sep in norm: 75 raise InsecureError("Child name '%s' contains one or more " 76 "directory separators" % (name, )) 77 childpath = os.path.abspath(os.path.join(self.path, norm)) 78 if not childpath.startswith(self.path): 79 raise InsecureError("Path '%s' is not a child of '%s'" 80 % (childpath, self.path)) 81 return childpath
82