def get_attachments(self, fields, request): """Return all attachments uploaded in form. """ from ZPublisher.HTTPRequest import FileUpload attachments = [] for field in fields: if (field.isFileField() or field.Type() == 'DataGrid Field') \ and (getattr(self, 'showAll', True) \ or field.fgField.getName() in getattr(self, 'showFields', ())): if field.Type() == 'DataGrid Field': # check if it contains any File columns for c in field.columnDefs: if c['columnType'] == 'File': for row in request.form.get('%s' % field.__name__, None): if row['orderindex_'] != 'template_row_marker': file = row[c['columnId']] if file and isinstance(file, FileUpload) \ and file.filename != '': file.seek(0) # rewind data = file.read() filename = file.filename mimetype, enc = guess_content_type(filename, data, None) attachments.append((filename, mimetype, enc, data)) else: file = request.form.get('%s_file' % field.__name__, None) if file and isinstance(file, FileUpload) and file.filename != '': file.seek(0) # rewind data = file.read() filename = file.filename mimetype, enc = guess_content_type(filename, data, None) attachments.append((filename, mimetype, enc, data)) return attachments
def test_add_two_files(self): ntypes = len(mimetypes.types_map) contenttype.add_files([MIME_TYPES_1, MIME_TYPES_2]) ctype, encoding = contenttype.guess_content_type("foo.ztmt-1") self.assert_(encoding is None) self.assertEqual(ctype, "text/x-vnd.zope.test-mime-type-1") ctype, encoding = contenttype.guess_content_type("foo.ztmt-2") self.assert_(encoding is None) self.assertEqual(ctype, "text/x-vnd.zope.test-mime-type-2") self.check_types_count(2)
def test_guess_content_type(self): from zope.contenttype import add_files from zope.contenttype import guess_content_type filename = self._getFilename('mime.types-1') add_files([filename]) ctype, encoding = guess_content_type(body=b'text file') self.assertEqual(ctype, "text/plain") ctype, encoding = guess_content_type(body=b'\001binary') self.assertEqual(ctype, "application/octet-stream") ctype, encoding = guess_content_type() self.assertEqual(ctype, "text/x-unknown-content-type")
def test_add_one_file(self): from zope.contenttype import add_files from zope.contenttype import guess_content_type filename = self._getFilename('mime.types-1') add_files([filename]) ctype, encoding = guess_content_type("foo.ztmt-1") self.assertTrue(encoding is None) self.assertEqual(ctype, "text/x-vnd.zope.test-mime-type-1") ctype, encoding = guess_content_type("foo.ztmt-1.gz") self.assertEqual(encoding, "gzip") self.assertEqual(ctype, "text/x-vnd.zope.test-mime-type-1") self._check_types_count(1)
def test_add_two_files(self): from zope.contenttype import add_files from zope.contenttype import guess_content_type filename1 = self._getFilename('mime.types-1') filename2 = self._getFilename('mime.types-2') add_files([filename1, filename2]) ctype, encoding = guess_content_type("foo.ztmt-1") self.assertTrue(encoding is None) self.assertEqual(ctype, "text/x-vnd.zope.test-mime-type-1") ctype, encoding = guess_content_type("foo.ztmt-2") self.assertTrue(encoding is None) self.assertEqual(ctype, "text/x-vnd.zope.test-mime-type-2") self._check_types_count(2)
def get_attachments(self, fields, request): """Return all attachments uploaded in form. """ from ZPublisher.HTTPRequest import FileUpload attachments = [] for field in fields: if (field.isFileField() or field.Type() == 'DataGrid Field') \ and (getattr(self, 'showAll', True) \ or field.fgField.getName() in getattr(self, 'showFields', ())): if field.Type() == 'DataGrid Field': # check if it contains any File columns for c in field.columnDefs: if c['columnType'] == 'File': for row in request.form.get( '%s' % field.__name__, None): if row['orderindex_'] != 'template_row_marker': file = row[c['columnId']] if file and isinstance(file, FileUpload) \ and file.filename != '': file.seek(0) # rewind data = file.read() filename = file.filename mimetype, enc = guess_content_type( filename, data, None) attachments.append( (filename, mimetype, enc, data)) else: file = request.form.get('%s_file' % field.__name__, None) if file and isinstance(file, FileUpload) and file.filename != '': file.seek(0) # rewind data = file.read() filename = file.filename filename_prefix_field = getattr( field, 'fgFileNamePrefix', u'') fnprefix = u'' if filename_prefix_field != u'': fnprefix = request.form.get( filename_prefix_field, '') if fnprefix: filename = '%s_%s' % (fnprefix, filename) mimetype, enc = guess_content_type( filename, data, None) attachments.append((filename, mimetype, enc, data)) return attachments
def onSuccess(self, fields, REQUEST=None, loopstop=False): # """ # saves data. # """ if LP_SAVE_TO_CANONICAL and not loopstop: # LinguaPlone functionality: # check to see if we're in a translated # form folder, but not the canonical version. parent = self.aq_parent if safe_hasattr(parent, 'isTranslation') and \ parent.isTranslation() and not parent.isCanonical(): # look in the canonical version to see if there is # a matching (by id) save-data adapter. # If so, call its onSuccess method cf = parent.getCanonical() target = cf.get(self.getId()) if target is not None and target.meta_type == 'FormSaveDataAdapter': target.onSuccess(fields, REQUEST, loopstop=True) return from ZPublisher.HTTPRequest import FileUpload data = [] for f in fields: showFields = getattr(self, 'showFields', []) if showFields and f.id not in showFields: continue if f.isFileField(): file = REQUEST.form.get('%s_file' % f.fgField.getName()) if isinstance(file, FileUpload) and file.filename != '': file.seek(0) fdata = file.read() filename = file.filename mimetype, enc = guess_content_type(filename, fdata, None) if mimetype.find('text/') >= 0: # convert to native eols fdata = fdata.replace('\x0d\x0a', '\n').replace('\x0a', '\n').replace('\x0d', '\n') data.append('%s:%s:%s:%s' % (filename, mimetype, enc, fdata)) else: data.append('%s:%s:%s:Binary upload discarded' % (filename, mimetype, enc)) else: data.append('NO UPLOAD') elif not f.isLabel(): val = REQUEST.form.get(f.fgField.getName(), '') if not type(val) in StringTypes: # Zope has marshalled the field into # something other than a string val = str(val) data.append(val) if self.ExtraData: for f in self.ExtraData: if f == 'dt': data.append(str(DateTime())) else: data.append(getattr(REQUEST, f, '')) self._addDataRow(data)
def setFile(self, value, **kwargs): request = self.REQUEST tramlined = request.get('HTTP_X_TRAMLINED', '') if not tramlined or (value == ''): self.o_setFile(value, **kwargs) else: fileinfo = value.read().split('|', 1) filepath = fileinfo[0] filesize = fileinfo[-1] filename = value.filename mimetype, enc = guess_content_type(filename) self.tramline_size = int(filesize) if self.tramline_size > 32000000: raise "上传的文件大小超过 30M 限制,上传失败!" self.setContentType(mimetype) #用于修订IE的文件带路径问题 title = filename.split("\\")[-1] self.setFilename(title) self.setTitle(title) # 是否只上传1个文件? tramline_ok = request.response.getHeader('tramline_ok') or '' # 不是第一文件了,需要用分隔符合并起来 if tramline_ok: tramline_ok += '|' tramline_ok += '%s:%s:%s' % (filepath,\ '/'.join(self.getPhysicalPath()), '') event.notify(ObjectEditedEvent(self)) request.response.setHeader('tramline_ok', tramline_ok)
def setImage(self, value, refresh_exif=True, **kwargs): request = self.REQUEST tramlined = request.get('HTTP_X_TRAMLINED', '') if not tramlined or (value == ''): self.o_setImage(value, refresh_exif, **kwargs) else: fileinfo = value.read().split('|', 1) filepath = fileinfo[0] filesize = fileinfo[-1] filename = value.filename mimetype, enc = guess_content_type(filename) self.tramline_size = int(filesize) if self.tramline_size > 32000000: raise "上传的文件大小超过 30M 限制,上传失败!" self.setContentType(mimetype) #用于修订IE的文件带路径问题 #不能用setFilename,它会失效,因为通过tramline后,图片为空, #ImageField不会生成空的图片 title = filename.split("\\")[-1] self.setTitle(title) tramline_ok = request.response.getHeader('tramline_ok') or '' if tramline_ok: tramline_ok += '|' tramline_ok += '%s:%s:%s' % (filepath,\ '/'.join(self.getPhysicalPath()), 's') event.notify(ObjectEditedEvent(self)) request.response.setHeader('tramline_ok', tramline_ok)
def htmlValue(self, REQUEST): """ return from REQUEST, this field's value in html. """ value = REQUEST.form.get(self.__name__, 'No Input') header = '' for col in self.columnDefs: header += "<th>%s</th>" % col['columnTitle'] res = '<table class="listing"><thead><tr>%s</tr></thead><tbody>' % header for adict in value: if adict.get('orderindex_', '') != 'template_row_marker': res += "<tr>" for col in self.columnDefs: akey = col['columnId'] if col['columnType'] == "File": file = adict[akey] file.seek(0) fdata = file.read() filename = file.filename mimetype, enc = guess_content_type( filename, fdata, None) out = "%s %s: %s bytes" % (filename, mimetype, len(fdata)) res = "%s\n<td>%s</td>" % (res, cgi.escape(out)) else: res = "%s\n<td>%s</td>" % (res, cgi.escape( adict[akey])) res += "</tr>" return "%s</tbody></table>" % res
def htmlValue(self, REQUEST): """ return from REQUEST, this field's value in html. """ value = REQUEST.form.get(self.__name__, 'No Input') header = '' for col in self.columnDefs: header += "<th>%s</th>" % col['columnTitle'] res = '<table class="listing"><thead><tr>%s</tr></thead><tbody>' % header for adict in value: if adict.get('orderindex_', '') != 'template_row_marker': res += "<tr>" for col in self.columnDefs: akey = col['columnId'] if col['columnType'] == "File": file = adict[akey] file.seek(0) fdata = file.read() filename = file.filename mimetype, enc = guess_content_type(filename, fdata, None) out = "%s %s: %s bytes" % (filename, mimetype, len(fdata)) res = "%s\n<td>%s</td>" % (res, cgi.escape(out)) else: res = "%s\n<td>%s</td>" % (res, cgi.escape(adict[akey])) res += "</tr>" return "%s</tbody></table>" % res
def _get_content_type(self, file, body, id, content_type=None): # Consult self.content_type first, this is either # the default (unknown/unknown) or it got a value from a # .metadata file default_type = 'unknown/unknown' if getattr(self, 'content_type', default_type) != default_type: return self.content_type # Next, look at file headers headers = getattr(file, 'headers', None) if headers and headers.has_key('content-type'): content_type = headers['content-type'] else: # Last resort: Use the (imperfect) content type guessing # mechanism from OFS.Image, which ultimately uses the # Python mimetypes module. if not isinstance(body, basestring): body = body.data content_type, enc = guess_content_type( getattr(file, 'filename', id), body, content_type) if (enc is None and (content_type.startswith('text/') or content_type.startswith('application/')) and body.startswith(codecs.BOM_UTF8)): content_type += '; charset=utf-8' return content_type
def getContents(self): ##print "***** READ", self.path f = open(self.path, 'rb') data = f.read() f.close() content_type, enc = guess_content_type(self.path, data) return dict(data = data, content_type = content_type)
def _addFileFromRequest(self,REQUEST,log=''): """Add and link a new File or Image object, depending on file's filename suffix. Returns a tuple containing the new id, content type & size, or (None,None,None). """ # ensure we have a suitable file, id and title file, title = REQUEST.file, str(REQUEST.get('title','')) id, title = OFS.Image.cookId('', title, file) if not id: return None folder = self.uploadFolder() try: checkValidId(folder,id,allow_dup=1) except BadRequest: id, ext = os.path.splitext(id) id = self.canonicalIdFrom(id) id = id + ext # create the file or image object, unless it already exists # XXX should use CMF/Plone content types when appropriate if not (self.folderContains(folder,id) and folder[id].meta_type in ('File','Image')): #'Portal File','Portal Image')): if guess_content_type(file.filename)[0][0:5] == 'image': # if self.inCMF(): # #from Products.ATContentTypes import ATFile, ATImage # #folder.create(ATImage) ... # else: id = folder._setObject(id, OFS.Image.Image(id,title,'')) else: # if self.inCMF(): # else: id = folder._setObject(id, OFS.Image.File(id,title,'')) # adding the data after creation is more efficient, reportedly ob = folder._getOb(id) ob.manage_upload(file) # and link/inline it self._addFileOrImageToPage(id,ob.content_type,ob.getSize(),log,REQUEST) return id
def _get_content_type(self, file, body, id, content_type=None): # Consult self.content_type first, this is either # the default (unknown/unknown) or it got a value from a # .metadata file default_type = 'unknown/unknown' if getattr(self, 'content_type', default_type) != default_type: return self.content_type # Next, look at file headers headers=getattr(file, 'headers', None) if headers and headers.has_key('content-type'): content_type=headers['content-type'] else: # Last resort: Use the (imperfect) content type guessing # mechanism from OFS.Image, which ultimately uses the # Python mimetypes module. if not isinstance(body, basestring): body = body.data content_type, enc=guess_content_type( getattr(file, 'filename',id), body, content_type) if (enc is None and (content_type.startswith('text/') or content_type.startswith('application/')) and body.startswith(codecs.BOM_UTF8)): content_type += '; charset=utf-8' return content_type
def my_fetcher(url): uri = url # Otherwise fetch the data response = subrequest(unquote(uri)) # Handle redirects if response.status == 301: uri = response.getHeader('location') response = subrequest(unquote(uri)) if response.status != 200: raise Exception("URI not found") content_type = response.getHeader('content-type') # Default encoding encoding = 'utf-8' if content_type: if ';' in content_type: ctype, encoding = content_type.split(';') encoding = encoding.split('charset=')[-1] else: ctype = content_type # Guess the content_type from the URI if needed else: ctype, encoding = guess_content_type(uri) if ctype and ctype.startswith('text/'): ctype = text_type(uri) data = response.getBody() # I don't think we need to encode ctype == 'text/css' to ascii anymore return dict(string=data, mime_type=ctype, encoding=encoding)
def __call__(self, client=None, REQUEST={}, RESPONSE=None, **kw): """Render the document with the given client object. o If supplied, use REQUEST mapping, Response, and key word arguments. """ if not self._cache_namespace_keys: data = self.ZCacheable_get(default=_marker) if data is not _marker: # Return cached results. return data __traceback_supplement__ = (PathTracebackSupplement, self) kw['document_id'] = self.getId() kw['document_title'] = self.title if hasattr(self, 'aq_explicit'): bself = self.aq_explicit else: bself = self security = getSecurityManager() security.addContext(self) try: if client is None: # Called as subtemplate, so don't need error propagation! r = HTML.__call__(self, bself, REQUEST, **kw) if RESPONSE is None: result = r else: result = decapitate(r, RESPONSE) if not self._cache_namespace_keys: self.ZCacheable_set(result) return result r = HTML.__call__(self, (client, bself), REQUEST, **kw) if RESPONSE is None or not isinstance(r, str): if not self._cache_namespace_keys: self.ZCacheable_set(r) return r finally: security.removeContext(self) have_key = RESPONSE.headers.__contains__ if not (have_key('content-type') or have_key('Content-Type')): if 'content_type' in self.__dict__: c = self.content_type else: encoding = getattr(self, 'encoding', default_encoding) if six.PY2 and not isinstance(r, six.text_type): # Prevent double-encoding edge cases under Python 2 r = r.decode(encoding) c, e = guess_content_type(self.getId(), r.encode(encoding)) RESPONSE.setHeader('Content-Type', c) result = decapitate(r, RESPONSE) if not self._cache_namespace_keys: self.ZCacheable_set(result) return result
def actual_process(self, data): cols = data['columns'] csv = BytesIO(data['csv']) # The file is Bytes, encoded. encoding = self.guess_encoding(data['csv']) # TODO: Delivery? try: dialect = Sniffer().sniff(csv.readline(), [',', '\t']) except err: dialect = excel csv.seek(0) reader = UnicodeDictReader(csv, cols, encoding=encoding, dialect=dialect) profiles = [] retval = None try: next(reader) # Skip the first row (the header) except UnicodeDecodeError as e: t = guess_content_type(body=data['csv'])[0] msg = 'The file is different from what is required. (It '\ 'appears to be a {0} file.) Please check that you '\ 'selected the correct CSV file.' m = {'status': -2, 'message': [msg.format(t.split('/')[0]), str(e), t]} retval = to_json(m) except StopIteration: msg = 'The file appears to be empty. Please check that you '\ 'generated the CSV file correctly.' m = {'status': -5, 'message': [msg, 'no-rows']} retval = to_json(m) else: rowCount = 0 for row in reader: rowCount += 1 if len(row) != len(cols): # *Technically* the number of columns in CSV rows can be # arbitary. However, I am enforcing a strict # interpretation for sanity's sake. msg = 'Row {0} had {1} columns, rather than {2}. ' \ 'Please check the file.' # Name hack. m = {'status': -3, 'message': [msg.format(rowCount, len(row), len(cols))]} retval = to_json(m) profiles = [] # --=mpj17=-- I think this is the first time I have used # break in actual code. Wow. break profiles.append(row) if profiles and (not retval): retval = to_json(profiles) elif (not profiles) and not(retval): msg = 'No rows were found in the CSV file. '\ 'Please check that you selected the correct CSV file.' m = {'status': -4, 'message': [msg, 'no-rows']} retval = to_json(m) assert retval, 'No retval' return retval
def __call__(self, name, content_type, data): if not content_type and data: content_type, width, height = getImageInfo(data) if not content_type: content_type, encoding = guess_content_type(name, data, '') if content_type.startswith('image/'): return Image(data) return File(data, content_type)
def _buildSObjectFromForm(self, fields, REQUEST=None): """ Used by the onSuccess handler to convert the fields from the form into the fields to be stored in Salesforce. Also munges dates into the required (mm/dd/yyyy) format. """ logger.debug('Calling _buildSObjectFromForm()') formPath = aq_parent(self).getPhysicalPath() sObject = dict(type=self.SFObjectType) for field in fields: formFieldPath = field.getPhysicalPath() formFieldValue = REQUEST.form.get(field.getFieldFormName()) if field.meta_type == 'FormDateField': if formFieldValue: formFieldValue = DateTime(formFieldValue + ' GMT+0').HTML4() else: # we want to throw this value away rather than pass along # to salesforce, which would ultimately raise a SoapFaultError # due to invalid xsd:dateTime formatting continue elif field.isFileField(): file = formFieldValue if file and isinstance(file, FileUpload) and file.filename != '': file.seek(0) # rewind data = file.read() filename = file.filename mimetype, enc = guess_content_type(filename, data, None) from base64 import encodestring formFieldValue = encodestring(data) filenameFieldName = self._getSFFieldForFormField(list(formFieldPath) + ['filename'], formPath) if filenameFieldName: sObject[filenameFieldName] = filename mimetypeFieldName = self._getSFFieldForFormField(list(formFieldPath) + ['mimetype'], formPath) if mimetypeFieldName: sObject[mimetypeFieldName] = mimetype salesforceFieldName = self._getSFFieldForFormField(formFieldPath, formPath) if not salesforceFieldName: # We haven't found a mapping to a Salesforce field. continue if 'creationMode' in self.Schema() and self.getCreationMode() == 'update' and formFieldValue == '': # The adapter is in update mode and one of the fields has a value # of an empty string. If that field is nillable in Salesforce, we # should set its value to None so that it gets cleared. salesforceField = self._querySFFieldsForType()[salesforceFieldName] if getattr(salesforceField, 'nillable', False): formFieldValue = None elif formFieldValue is None: # The form field was left blank and we therefore # don't care about passing along that value, since # the Salesforce object field may have it's own ideas # about data types and or default values. continue sObject[salesforceFieldName] = formFieldValue return sObject
def __init__(self, path, name): self.path = path self.__name__ = name with open(path, 'rb') as f: self.data = f.read() self.content_type = guess_content_type(path, self.data)[0] self.lmt = float(os.path.getmtime(path)) or time.time() self.lmh = formatdate(self.lmt, usegmt=True)
def onSuccess(self, fields, REQUEST=None, loopstop=False): """ saves data. """ if LP_SAVE_TO_CANONICAL and not loopstop: # LinguaPlone functionality: # check to see if we're in a translated # form folder, but not the canonical version. parent = self.aq_parent if safe_hasattr(parent, 'isTranslation') and \ parent.isTranslation() and not parent.isCanonical(): # look in the canonical version to see if there is # a matching (by id) save-data adapter. # If so, call its onSuccess method cf = parent.getCanonical() target = cf.get(self.getId()) if target is not None and target.meta_type == 'FormSaveDataAdapter': target.onSuccess(fields, REQUEST, loopstop=True) return from ZPublisher.HTTPRequest import FileUpload data = [] for f in fields: if f.isFileField(): file = REQUEST.form.get('%s_file' % f.fgField.getName()) if isinstance(file, FileUpload) and file.filename != '': file.seek(0) fdata = file.read() filename = file.filename mimetype, enc = guess_content_type(filename, fdata, None) if mimetype.find('text/') >= 0: # convert to native eols fdata = fdata.replace('\x0d\x0a', '\n').replace('\x0a', '\n').replace('\x0d', '\n') data.append( '%s:%s:%s:%s' % (filename, mimetype, enc, fdata) ) else: data.append( '%s:%s:%s:Binary upload discarded' % (filename, mimetype, enc) ) else: data.append( 'NO UPLOAD' ) elif not f.isLabel(): val = REQUEST.form.get(f.fgField.getName(),'') if not type(val) in StringTypes: # Zope has marshalled the field into # something other than a string val = str(val) data.append(val) if self.ExtraData: for f in self.ExtraData: if f == 'dt': data.append( str(DateTime()) ) else: data.append( getattr(REQUEST, f, '') ) self._addDataRow( data )
def getInputValue(self): self._error = None action = self.action_widget.getInputValue() form = self.request.form_ng if action == 'change' and not form.getOne(self.image_widget.name): self._error = WidgetInputError( self.name, self.label, LaunchpadValidationError( _('Please specify the image you want to use.'))) raise self._error if action == "keep": if self.style == self.ADD_STYLE: # It doesn't make any sense to return KEEP_SAME_IMAGE in this # case, since there's nothing to keep. return None elif self.style == self.EDIT_STYLE: return KEEP_SAME_IMAGE else: raise AssertionError( "Style must be one of EDIT_STYLE or ADD_STYLE, got %s" % self.style) elif action == "change": self._image = form.getOne(self.image_widget.name) try: self.context.validate(self._image) except ValidationError as v: self._error = WidgetInputError(self.name, self.label, v) raise self._error self._image.seek(0) content = self._image.read() filename = self._image.filename type, dummy = guess_content_type(name=filename, body=content) # This method may be called more than once in a single request. If # that's the case here we'll simply return the cached # LibraryFileAlias we already have. existing_alias = self._image_file_alias if existing_alias is not None: assert existing_alias.filename == filename, ( "The existing LibraryFileAlias' name doesn't match the " "given image's name.") assert existing_alias.content.filesize == len(content), ( "The existing LibraryFileAlias' size doesn't match " "the given image's size.") assert existing_alias.mimetype == type, ( "The existing LibraryFileAlias' type doesn't match " "the given image's type.") return existing_alias self._image_file_alias = getUtility(ILibraryFileAliasSet).create( name=filename, size=len(content), file=StringIO(content), contentType=type) return self._image_file_alias elif action == "delete": return None
def __call__(self, name, content_type, data): if not content_type and data: content_type, width, height = getImageInfo(data) if not content_type: content_type, encoding = guess_content_type(name, '', '') res = LocalFsFile( name, os.path.join(self.context.abspath, name), content_type) res.__parent__ = self.context return res
def _get_content_type(self, file, body, id, content_type=None): headers = getattr(file, "headers", None) if headers and "content-type" in headers: content_type = headers["content-type"] else: if not isinstance(body, str): body = body.data content_type, enc = guess_content_type(getattr(file, "filename", id), body, content_type) return content_type
def _get_content_type(self, file, body, id, content_type=None): headers=getattr(file, 'headers', None) if headers and 'content-type' in headers: content_type=headers['content-type'] else: if not isinstance(body, str): body=body.data content_type, enc=guess_content_type( getattr(file, 'filename',id), body, content_type) return content_type
def _get_content_type(self, file, body, id, content_type=None): headers = getattr(file, 'headers', None) if headers and 'content-type' in headers: content_type = headers['content-type'] else: if not isinstance(body, str): body = body.data content_type, enc = guess_content_type( getattr(file, 'filename', id), body, content_type) return content_type
def __init__(self, data='', content_type='', filename=None): self.data = data if filename is not None: self.filename = clean_filename(filename) if not content_type and filename: # If we handle large files, we don't want them read just # to guess the content type. We provide only the filename. self.content_type, enc = guess_content_type(name=filename) else: self.content_type = content_type
def __init__(self, path, name): self.path = path f = open(path, 'rb') data = f.read() f.close() self.content_type, enc = guess_content_type(path, data) self.__name__ = name self.lmt = float(os.path.getmtime(path)) or time() self.lmh = rfc1123_date(self.lmt)
def __call__(self, name, content_type, data): if not content_type and data: content_type, _width, _height = getImageInfo(data) if not content_type: content_type, _encoding = guess_content_type(name, data, '') if content_type.startswith('image/'): return Image(data) return File(data, content_type)
def my_guess_content_type(path, data): content_type, enc = guess_content_type(path, data) if content_type in ('text/plain', 'text/html'): if os.path.basename(path).endswith('.js-slimmed'): content_type = 'application/x-javascript' elif os.path.basename(path).find('.css-slimmed') > -1: # the find() covers both 'foo.css-slimmed' and # 'foo.css-slimmed-data64expanded' content_type = 'text/css' return content_type, enc
def __init__(self, path, name): self.path = path f = open(path, 'rb') self.data = f.read() f.close() self.content_type, enc = guess_content_type(path, self.data) self.__name__ = name self.lmt = float(os.path.getmtime(path)) or time.time() self.lmh = rfc1123_date(self.lmt)
def classify(self, data, mimetype=None, filename=None): """Classify works as follows: 1) you tell me the rfc-2046 name and I give you an IMimetype object 2) the filename includes an extension from which we can guess the mimetype 3) we can optionally introspect the data 4) default to self.defaultMimetype if no data was provided else to application/octet-stream of no filename was provided, else to text/plain Return an IMimetype object or None """ mt = None if mimetype: mt = self.lookup(mimetype) if mt: mt = mt[0] elif filename: mt = self.lookupExtension(filename) if mt is None: mt = self.globFilename(filename) if data and not mt: for c in self._classifiers(): if c.classify(data): mt = c break if not mt: mstr = magic.guessMime(data) if mstr: _mt = self.lookup(mstr) if len(_mt) > 0: mt = _mt[0] if not mt: if not data: mtlist = self.lookup(self.defaultMimetype) elif filename: mtlist = self.lookup('application/octet-stream') else: failed = 'text/x-unknown-content-type' filename = filename or '' data = data or '' if six.PY3: data = data.encode() ct, enc = guess_content_type(filename, data, None) if ct == failed: ct = 'text/plain' mtlist = self.lookup(ct) if len(mtlist) > 0: mt = mtlist[0] else: return None # Remove acquisition wrappers return aq_base(mt)
def actual_process(self, data): # TODO: Delivery? cols = data['columns'] csv = BytesIO(data['csv']) # The file is Bytes, encoded. reader = UnicodeDictReader(csv, cols) profiles = [] retval = None try: next(reader) # Skip the first row (the header) except UnicodeDecodeError as e: t = guess_content_type(body=data['csv'])[0] msg = 'The file is different from what is required. (It '\ 'appears to be a {0} file.) Please check that you '\ 'selected the correct CSV file.' m = { 'status': -2, 'message': [msg.format(t.split('/')[0]), str(e), t] } retval = to_json(m) except StopIteration: msg = 'The file appears to be empty. Please check that you '\ 'generated the CSV file correctly.' m = {'status': -5, 'message': [msg, 'no-rows']} retval = to_json(m) else: rowCount = 0 for row in reader: rowCount += 1 if len(row) != len(cols): # *Technically* the number of columns in CSV rows can be # arbitary. However, I am enforcing a strict # interpretation for sanity's sake. msg = 'Row {0} had {1} columns, rather than {2}. ' \ 'Please check the file.' # Name hack. m = { 'status': -3, 'message': [msg.format(rowCount, len(row), len(cols))] } retval = to_json(m) profiles = [] # --=mpj17=-- I think this is the first time I have used # break in actual code. Wow. break profiles.append(row) if profiles and (not retval): retval = to_json(profiles) elif (not profiles) and not (retval): msg = 'No rows were found in the CSV file. '\ 'Please check that you selected the correct CSV file.' m = {'status': -4, 'message': [msg, 'no-rows']} retval = to_json(m) assert retval, 'No retval' return retval
def __init__(self, path, _prefix=None): import Globals # for data if _prefix is None: _prefix = getattr(getConfiguration(), 'softwarehome', None) or PREFIX if not os.path.isabs(path): warnings.warn(NON_PREFIX_WARNING, UserWarning, 2) elif type(_prefix) is not str: _prefix = package_home(_prefix) # _prefix is ignored if path is absolute path = os.path.join(_prefix, path) self.path = path if Globals.DevelopmentMode: # In development mode, a shorter time is handy max_age = 60 # One minute else: # A longer time reduces latency in production mode max_age = 3600 # One hour self.cch = 'public,max-age=%d' % max_age # First try to get the content_type by name content_type, enc = guess_content_type(path, default='failed') if content_type == 'failed': # This failed, lets look into the file content img = open(path, 'rb') data = img.read(1024) # 1k should be enough img.close() content_type, enc = guess_content_type(path, data) if content_type: self.content_type = content_type else: ext = os.path.splitext(path)[-1].replace('.', '') self.content_type = 'image/%s' % ext self.__name__ = os.path.split(path)[-1] stat_info = os.stat(path) self.size = stat_info[stat.ST_SIZE] self.lmt = float(stat_info[stat.ST_MTIME]) or time.time() self.lmh = rfc1123_date(self.lmt)
def htmlValue(self, REQUEST): file = REQUEST.form.get('%s_file' % self.fgField.getName()) if isinstance(file, FileUpload) and file.filename != '': file.seek(0) fdata = file.read() filename = file.filename mimetype, enc = guess_content_type(filename, fdata, None) return "%s: %s bytes" % (mimetype, len(fdata)) else: return 'No Input'
def validate_content_types(self, instance, value, errors): """make sure the value's content-type is allowed""" if value in ("DELETE_IMAGE", "DELETE_FILE", None, ''): return None # plone.app.blob.field.BlobWrapper cannot be imported # at startup due to circular imports from plone.app.blob.field import BlobWrapper body = '' if isinstance(value, FileType): tell = value.tell() value.seek(0) body = value.read() value.seek(tell) elif isinstance(value, StringType): body = value elif isinstance(value, BlobWrapper): body = value.data if isinstance(value, (FileType, BlobWrapper)) and body in (None, ''): return None mtr = getToolByName(instance, 'mimetypes_registry', None) if mtr is not None: orig_filename = getattr(value, 'filename', getattr(value, 'name', '')) kw = dict(mimetype=None, filename=orig_filename) try: d, f, mimetype = mtr(body[:8096], **kw) except UnicodeDecodeError: d, f, mimetype = mtr(len(body) < 8096 and body or '', **kw) else: mimetype, enc = guess_content_type( value.filename, value.read(), None) mimetype = str(mimetype).split(';')[0].strip() if mimetype not in self.allowable_content_types: request = aq_get(instance, 'REQUEST') label = self.widget.Label(instance) name = self.getName() if isinstance(label, Message): label = translate(label, context=request) error = _(u'error_allowable_content_types', default=u'Mimetype ${mimetype} is not allowed ' 'on ${name}, please correct.', mapping={ 'mimetype': mimetype, 'name': label }) error = translate(error, context=request) errors[name] = error return error return
def __init__(self, path, _prefix=None): import Globals # for data if _prefix is None: _prefix=getattr(getConfiguration(), 'softwarehome', None) or PREFIX if not os.path.isabs(path): warnings.warn(NON_PREFIX_WARNING, UserWarning, 2) elif type(_prefix) is not str: _prefix=package_home(_prefix) # _prefix is ignored if path is absolute path = os.path.join(_prefix, path) self.path=path if Globals.DevelopmentMode: # In development mode, a shorter time is handy max_age = 60 # One minute else: # A longer time reduces latency in production mode max_age = 3600 # One hour self.cch = 'public,max-age=%d' % max_age # First try to get the content_type by name content_type, enc=guess_content_type(path, default='failed') if content_type == 'failed': # This failed, lets look into the file content img = open(path, 'rb') data = img.read(1024) # 1k should be enough img.close() content_type, enc=guess_content_type(path, data) if content_type: self.content_type=content_type else: ext = os.path.splitext(path)[-1].replace('.', '') self.content_type = 'image/%s' % ext self.__name__ = os.path.split(path)[-1] stat_info = os.stat(path) self.size = stat_info[stat.ST_SIZE] self.lmt = float(stat_info[stat.ST_MTIME]) or time.time() self.lmh = rfc1123_date(self.lmt)
def guess_type(filename, body): # check for XML ourself since guess_content_type can't # detect text/xml if 'filename' won't end with .xml # XXX: fix this in zope.contenttype if body.startswith(b'<?xml'): return 'text/xml' content_type, ignored_encoding = guess_content_type(filename, body) if content_type in ('text/html', 'text/xml'): return content_type return sniff_type(body) or 'text/html'
def guessMimetype(data, filename=None): """ guess the mime-type from the given file-like object, optionally using the filename as a hint; the current position in the file is tried to be preserved """ pos = data.tell() mtr = queryUtility(IMimetypesRegistryTool) if mtr is not None: d, f, mimetype = mtr(data.read(1 << 14), mimetype=None, filename=filename) else: mimetype, enc = guess_content_type(filename or '', data.read(), default=None) data.seek(pos) return str(mimetype)
def PUT_factory(self, name, typ, body): """ Hook PUT creation to make objects of the right type when new item uploaded via FTP/WebDAV. """ if typ is None: typ, enc = guess_content_type() if typ == 'text/x-python': return PythonScript(name) if typ[:4] == 'text': return DTMLMethod('', __name__=name) return None # take the default, then
def _get_content_type(self, file, body, id, content_type=None): """ Determine the mime-type """ headers = getattr(file, "headers", None) if headers and headers.has_key("content-type"): content_type = headers["content-type"] else: filename = getattr(file, "filename", None) or id content_type, enc = guess_content_type(filename, body, content_type) cutoff = content_type.find(";") if cutoff >= 0: return content_type[:cutoff] return content_type
def _get_content_type(self, file, body, id, content_type=None): """ Determine the mime-type """ headers = getattr(file, 'headers', None) if headers and headers.has_key('content-type'): content_type = headers['content-type'] else: filename = getattr(file, 'filename', None) or id content_type, enc = guess_content_type(filename, body, content_type) cutoff = content_type.find(';') if cutoff >= 0: return content_type[:cutoff] return content_type
def classify(self, data, mimetype=None, filename=None): """Classify works as follows: 1) you tell me the rfc-2046 name and I give you an IMimetype object 2) the filename includes an extension from which we can guess the mimetype 3) we can optionally introspect the data 4) default to self.defaultMimetype if no data was provided else to application/octet-stream of no filename was provided, else to text/plain Return an IMimetype object or None """ mt = None if mimetype: mt = self.lookup(mimetype) if mt: mt = mt[0] elif filename: mt = self.lookupExtension(filename) if mt is None: mt = self.globFilename(filename) if data and not mt: for c in self._classifiers(): if c.classify(data): mt = c break if not mt: mstr = magic.guessMime(data) if mstr: _mt = self.lookup(mstr) if len(_mt) > 0: mt = _mt[0] if not mt: if not data: mtlist = self.lookup(self.defaultMimetype) elif filename: mtlist = self.lookup('application/octet-stream') else: failed = 'text/x-unknown-content-type' filename = filename or '' data = data or '' ct, enc = guess_content_type(filename, data, None) if ct == failed: ct = 'text/plain' mtlist = self.lookup(ct) if len(mtlist) > 0: mt = mtlist[0] else: return None # Remove acquisition wrappers return aq_base(mt)
def guess_type(filename, text): # check for XML ourself since guess_content_type can't # detect text/xml if 'filename' won't end with .xml # XXX: fix this in zope.contenttype if text.startswith('<?xml'): return 'text/xml' content_type, dummy = guess_content_type(filename, text) if content_type in ('text/html', 'text/xml'): return content_type return sniff_type(text) or 'text/html'
def __call__(self, client=None, REQUEST={}, RESPONSE=None, **kw): """Render the document with the given client object. o If supplied, use REQUEST mapping, Response, and key word arguments. """ if not self._cache_namespace_keys: data = self.ZCacheable_get(default=_marker) if data is not _marker: # Return cached results. return data __traceback_supplement__ = (PathTracebackSupplement, self) kw['document_id'] = self.getId() kw['document_title'] = self.title if hasattr(self, 'aq_explicit'): bself = self.aq_explicit else: bself = self security = getSecurityManager() security.addContext(self) try: if client is None: # Called as subtemplate, so don't need error propigation! r = apply(HTML.__call__, (self, bself, REQUEST), kw) if RESPONSE is None: result = r else: result = decapitate(r, RESPONSE) if not self._cache_namespace_keys: self.ZCacheable_set(result) return result r = apply(HTML.__call__, (self, (client, bself), REQUEST), kw) if type(r) is not type('') or RESPONSE is None: if not self._cache_namespace_keys: self.ZCacheable_set(r) return r finally: security.removeContext(self) have_key = RESPONSE.headers.has_key if not (have_key('content-type') or have_key('Content-Type')): if self.__dict__.has_key('content_type'): c = self.content_type else: c, e = guess_content_type(self.__name__, r) RESPONSE.setHeader('Content-Type', c) result = decapitate(r, RESPONSE) if not self._cache_namespace_keys: self.ZCacheable_set(result) return result
def upload(bucket, *, path=None, key=None): key = key or path_to_key(path) path = path or key_to_path(key) content = path.read_bytes() content_type, encoding = guess_content_type(str(path), content) print(f"Uploading {key} ({content_type}, {encoding})") bucket.put_object( Key=key, Body=content, ContentType=content_type, ContentEncoding=encoding or "", )
def errors(self): form = self.request.form if "UPDATE_SUBMIT" in form: filename = getattr(form["field.data"], "filename", None) contenttype = form.get("field.contentType") if filename: filename = cleanupFileName(filename) if not contenttype: contenttype = guess_content_type(filename)[0] if not form.get("add_input_name"): form["add_input_name"] = filename return self.update_object(form["field.data"], contenttype) return ''
def get_attachments(self, fields, request): """Return all attachments uploaded in form. """ attachments = [] # if requested, generate CSV attachment of form values sendCSV = getattr(self, 'sendCSV', None) if sendCSV: csvdata = [] sendXML = getattr(self, 'sendXML', None) if sendXML: xmlRoot = ET.Element("form") for fname in fields: field = fields[fname] showFields = getattr(self, 'showFields', []) or [] if sendCSV: if not is_file_data(field) and (getattr(self, 'showAll', True) or fname in showFields): csvdata.append(field) if sendXML: if not is_file_data(field) and (getattr(self, 'showAll', True) or fname in showFields): ET.SubElement(xmlRoot, "field", name=fname).text \ = self.serialize(field) if is_file_data(field) and (getattr(self, 'showAll', True) or fname in showFields): data = field.data filename = field.filename mimetype, enc = guess_content_type(filename, data, None) attachments.append((filename, mimetype, enc, data)) if sendCSV: output = BytesIO() writer = csvwriter(output) writer.writerow(csvdata) csv = output.getvalue() now = DateTime().ISO().replace(' ', '-').replace(':', '') filename = 'formdata_{0}.csv'.format(now) attachments.append((filename, 'text/plain', 'utf-8', csv)) if sendXML: xmlstr = ET.tostring(xmlRoot, encoding='utf8', method='xml') now = DateTime().ISO().replace(' ', '-').replace(':', '') filename = 'formdata_{0}.xml'.format(now) attachments.append((filename, 'text/xml', 'utf-8', xmlstr)) return attachments