Back to index

plone3  3.1.7
urlupload.py
Go to the documentation of this file.
00001 #  ATContentTypes http://plone.org/products/atcontenttypes/
00002 #  Archetypes reimplementation of the CMF core types
00003 #  Copyright (c) 2003-2006 AT Content Types development team
00004 #
00005 #  This program is free software; you can redistribute it and/or modify
00006 #  it under the terms of the GNU General Public License as published by
00007 #  the Free Software Foundation; either version 2 of the License, or
00008 #  (at your option) any later version.
00009 #
00010 #  This program is distributed in the hope that it will be useful,
00011 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 #  GNU General Public License for more details.
00014 #
00015 #  You should have received a copy of the GNU General Public License
00016 #  along with this program; if not, write to the Free Software
00017 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 #
00019 """URL upload
00020 
00021 NOT WORKING! experimental code!
00022 """
00023 __author__  = 'Christian Heimes <tiran@cheimes.de>'
00024 __docformat__ = 'restructuredtext'
00025 
00026 import urllib2
00027 import urlparse
00028 
00029 from AccessControl import ClassSecurityInfo
00030 from Globals import InitializeClass
00031 from ExtensionClass import Base
00032 
00033 from Products.CMFCore.permissions import View
00034 
00035 from Products.Archetypes.atapi import StringField
00036 from Products.Archetypes.atapi import StringWidget
00037 from Products.ATContentTypes import permission as ATCTPermissions
00038 
00039 from Products.ATContentTypes import ATCTMessageFactory as _
00040 
00041 class InvalidContentType(Exception):
00042     """Invalid content type (uploadFromURL)
00043     """
00044 
00045 urlUploadField = StringField('urlUpload',
00046         required = False,
00047         mode = 'w', # write only field
00048         languageIndependent = True,
00049         validators = ('isURL',),
00050         write_permission = ATCTPermissions.UploadViaURL,
00051         widget = StringWidget(
00052             description=_(u'help_upload_url',
00053                           default=u'Upload a file from another server by url.'),
00054             label = _(u'label_upload_url', default=u'Upload from server'),
00055             visible={'view' : 'hidden',
00056                      'edit' : 'hidden'}
00057             ),
00058         )
00059 
00060 class URLUpload(Base):
00061     
00062     security = ClassSecurityInfo()
00063     
00064     security.declarePrivate('loadFileFromURL')
00065     def loadFileFromURL(self, url, contenttypes=()):
00066         """Loads a file from an url using urllib2
00067 
00068         You can use contenttypes to restrict uploaded content types like:
00069             ('image',) for all image content types
00070             ('image/jpeg', 'image/png') only jpeg and png
00071 
00072         May raise an urllib2.URLError based exception or InvalidContentType
00073 
00074         returns file_handler, mimetype, filename, size_in_bytes
00075         """
00076         fh = urllib2.urlopen(url)
00077 
00078         info = fh.info()
00079         mimetype = info.get('content-type', 'application/octetstream')
00080         size = info.get('content-length', None)
00081 
00082         # scheme, netloc, path, parameters, query, fragment
00083         path = urlparse.urlparse(fh.geturl())[2]
00084         if path.endswith('/'):
00085             pos = -2
00086         else:
00087             pos = -1
00088         filename = path.split('/')[pos]
00089 
00090         success = False
00091         for ct in contenttypes:
00092             if ct.find('/') == -1:
00093                 if mimetype[:mimetype.find('/')] == ct:
00094                     success = True
00095                     break
00096             else:
00097                 if mimetype == ct:
00098                     success = True
00099                     break
00100         if not contenttypes:
00101             success = True
00102         if not success:
00103             raise InvalidContentType, mimetype
00104 
00105         return fh, mimetype, filename, size
00106 
00107     security.declareProtected(ATCTPermissions.UploadViaURL, 'setUploadURL')
00108     def setUrlUpload(self, value, **kwargs):
00109         """Upload a file from URL
00110         """
00111         if not value:
00112             return
00113         # XXX no error catching
00114         fh, mimetype, filename, size = self.loadFileFromURL(value,
00115                                            contenttypes=('image',))
00116         mutator = self.getPrimaryField().getMutator(self)
00117         mutator(fh.read(), mimetype=mimetype, filename=filename)
00118 
00119     security.declareProtected(View, 'getUploadURL')
00120     def getUrlUpload(self, **kwargs):
00121         """Always return the default value since we don't store the url
00122         """
00123         return self.getField('urlUpload').default
00124 
00125 InitializeClass(URLUpload)