def print_attach(self, datfile, id, stamp, suffix, thumbnail_size=None): """Print attachment.""" cache = Cache(datfile) (type, null) = mimetypes.guess_type("test." + suffix) if type is None: type = "text/plain" if cache.has_record(): pass elif self.check_get_cache(): self.get_cache(cache) else: self.print404(cache) return rec = Record(datfile=cache.datfile, idstr=stamp + '_' + id) if not rec.exists(): self.print404(cache) return attach_file = rec.attach_path(suffix=suffix, thumbnail_size=thumbnail_size) if config.thumbnail_size is not None and not os.path.isfile( attach_file): if config.force_thumbnail or thumbnail_size == config.thumbnail_size: rec.make_thumbnail(suffix=suffix, thumbnail_size=thumbnail_size) if attach_file is not None: size = rec.attach_size(suffix=suffix, thumbnail_size=thumbnail_size) self.stdout.write("Content-Type: " + type + "\n" + "Last-Modified: " + self.rfc822_time(stamp) + "\n" + "Content-Length: " + str(size) + "\n") if not attachutil.is_valid_image(type, attach_file): self.stdout.write("Content-Disposition: attachment\n") self.stdout.write("\n") try: f = file(attach_file, "rb") buf = f.read(1024) while (buf != ""): self.stdout.write(buf) buf = f.read(1024) f.close() except IOError: self.print404(cache)
def print_attach(self, datfile, id, stamp, suffix, thumbnail_size=None): """Print attachment.""" cache = Cache(datfile) (type, null) = mimetypes.guess_type("test." + suffix) if type is None: type = "text/plain" if cache.has_record(): pass elif self.check_get_cache(): self.get_cache(cache) else: self.print404(cache) return rec = Record(datfile=cache.datfile, idstr=stamp+'_'+id) if not rec.exists(): self.print404(cache) return attach_file = rec.attach_path(suffix=suffix, thumbnail_size=thumbnail_size) if config.thumbnail_size is not None and not os.path.isfile(attach_file): if config.force_thumbnail or thumbnail_size == config.thumbnail_size: rec.make_thumbnail(suffix=suffix, thumbnail_size=thumbnail_size) if attach_file is not None: size = rec.attach_size(suffix=suffix, thumbnail_size=thumbnail_size) self.stdout.write( "Content-Type: " + type + "\n" + "Last-Modified: " + self.rfc822_time(stamp) + "\n" + "Content-Length: " + str(size) + "\n") if not attachutil.is_valid_image(type, attach_file): self.stdout.write("Content-Disposition: attachment\n") self.stdout.write("\n") try: f = file(attach_file, "rb") buf = f.read(1024) while (buf != ""): self.stdout.write(buf) buf = f.read(1024) f.close() except IOError: self.print404(cache)
def print_record(self, cache, rec, path, str_path): thumbnail_size = None if 'attach' in rec: attach_file = rec.attach_path() attach_size = rec.attach_size(attach_file) suffix = rec.get('suffix', '') if not re.search('^[0-9A-Za-z]+$', suffix): suffix = 'txt' (type, null) = mimetypes.guess_type("test." + suffix) if type is None: type = "text/plain" if attachutil.is_valid_image(type, attach_file): thumbnail_size = config.thumbnail_size else: attach_file = None attach_size = None suffix = None if 'body' in rec: body = rec['body'] else: body = '' body = self.html_format(body, self.thread_cgi, path) var = { 'cache': cache, 'rec': rec, 'sid': rec['id'][:8], 'path': path, 'str_path': str_path, 'attach_file': attach_file, 'attach_size': attach_size, 'suffix': suffix, 'body': body, 'res_anchor': self.res_anchor, 'thumbnail': thumbnail_size, } self.stdout.write(self.template('record', var))