def save_image(cls, filename, fp, content_type=None, thumbnail_size=None, thumbnail_meta=None, square=False, save_original=False, original_meta=None): if content_type is None: content_type = utils.guess_mime_type(filename) if not content_type.lower() in SUPPORTED_BY_PIL: return None, None image = Image.open(fp) format = image.format if save_original: original_meta = original_meta or {} original = cls(filename=filename, content_type=content_type, **original_meta) with original.wfile() as fp_w: if 'transparency' in image.info: image.save(fp_w, format, transparency=image.info['transparency']) else: image.save(fp_w, format) else: original = None thumbnail = cls.save_thumbnail(filename, image, content_type, thumbnail_size, thumbnail_meta, square) return original, thumbnail
def upload_attachments(page, pid, beginning): dirpath = os.path.join(options.output_dir, pid, 'wiki', beginning) if not os.path.exists(dirpath): return files = os.listdir(dirpath) for f in files: with open(os.path.join(options.output_dir, pid, 'wiki', beginning, f)) as fp: page.attach(f, fp, content_type=utils.guess_mime_type(f))
def save_image(cls, filename, fp, content_type=None, thumbnail_size=None, thumbnail_meta=None, square=False, save_original=False, original_meta=None, convert_bmp=False): if content_type is None: content_type = utils.guess_mime_type(filename) if not content_type.lower() in SUPPORTED_BY_PIL: log.debug('Content type %s from file %s not supported', content_type, filename) return None, None try: image = PIL.Image.open(fp) except IOError as e: log.error('Error opening image %s %s', filename, e, exc_info=True) return None, None format = image.format save_anim = False if format == 'BMP' and convert_bmp: # use jpg format if bitmap is provided format = 'PNG' content_type = 'image/png' filename = re.sub('.bmp$', '.png', filename, flags=re.IGNORECASE) if format == 'GIF': save_anim = True # save all frames if GIF is provided if save_original: original_meta = original_meta or {} original = cls(filename=filename, content_type=content_type, **original_meta) with original.wfile() as fp_w: try: if 'transparency' in image.info: image.save(fp_w, format, transparency=image.info['transparency'], save_all=save_anim) else: image.save(fp_w, format, save_all=save_anim) except Exception as e: session(original).expunge(original) log.error('Error saving image %s %s', filename, e) return None, None else: original = None thumbnail = cls.save_thumbnail(filename, image, content_type, thumbnail_size, thumbnail_meta, square) return original, thumbnail
def add_attachment(self, file_info): if hasattr(file_info, 'file'): mime_type = file_info.type if not mime_type or '/' not in mime_type: mime_type = utils.guess_mime_type(file_info.filename) self.attach( file_info.filename, file_info.file, content_type=mime_type, post_id=self._id, thread_id=self.thread_id, discussion_id=self.discussion_id)
def save_image(cls, filename, fp, content_type=None, thumbnail_size=None, thumbnail_meta=None, square=False, save_original=False, original_meta=None, convert_bmp=False): if content_type is None: content_type = utils.guess_mime_type(filename) if not content_type.lower() in SUPPORTED_BY_PIL: log.debug('Content type %s from file %s not supported', content_type, filename) return None, None try: image = PIL.Image.open(fp) except IOError as e: log.error('Error opening image %s %s', filename, e, exc_info=True) return None, None format = image.format save_anim = False if format == 'BMP' and convert_bmp: # use jpg format if bitmap is provided format = 'PNG' content_type = 'image/png' filename = re.sub('.bmp$', '.png', filename, flags=re.IGNORECASE) if format == 'GIF': save_anim = True # save all frames if GIF is provided if save_original: original_meta = original_meta or {} original = cls( filename=filename, content_type=content_type, **original_meta) with original.wfile() as fp_w: try: if 'transparency' in image.info: image.save(fp_w, format, transparency=image.info['transparency'], save_all=save_anim) else: image.save(fp_w, format, save_all=save_anim) except Exception as e: session(original).expunge(original) log.error('Error saving image %s %s', filename, e) return None, None else: original = None thumbnail = cls.save_thumbnail( filename, image, content_type, thumbnail_size, thumbnail_meta, square) return original, thumbnail
def attach(self, file_info=None): require_access(self.post, 'moderate') if hasattr(file_info, 'file'): mime_type = file_info.type # If mime type was not passed or bogus, guess it if not mime_type or '/' not in mime_type: mime_type = utils.guess_mime_type(file_info.filename) self.post.attach( file_info.filename, file_info.file, content_type=mime_type, post_id=self.post._id, thread_id=self.post.thread_id, discussion_id=self.post.discussion_id) redirect(request.referer)
def __init__(self, url, filename=None): extractor = ProjectExtractor(None, url, parser=stringio_parser) self.url = url self.filename = filename or os.path.basename(urlparse(url).path) # try to get the mime-type from the filename first, because # some files (e.g., attachements) may have the Content-Type header # forced to encourage the UA to download / save the file self.type = guess_mime_type(self.filename) if self.type == 'application/octet-stream': # however, if that fails, fall back to the given mime-type, # as some files (e.g., project icons) might have no file # extension but return a valid Content-Type header self.type = extractor.page['content-type'] self.file = extractor.page['data']
def attach(self, file_info=None): require_access(self.post, 'moderate') if hasattr(file_info, 'file'): mime_type = file_info.type # If mime type was not passed or bogus, guess it if not mime_type or '/' not in mime_type: mime_type = utils.guess_mime_type(file_info.filename) self.post.attach(file_info.filename, file_info.file, content_type=mime_type, post_id=self.post._id, thread_id=self.post.thread_id, discussion_id=self.post.discussion_id) redirect(request.referer)
def load_attachments(self, page_dir, page_title): """Load attachments for page. page_dir - path to directory with page dump. """ page = WM.Page.query.get(app_config_id=self.wiki.config._id, title=page_title) for filename, path in self._attachments(page_dir): try: with open(path) as fp: page.attach(filename, fp, content_type=utils.guess_mime_type(filename)) except IOError, e: log.error("Can't open file: %s", str(e)) raise
def load_attachments(self, page_dir, page_title): """Load attachments for page. page_dir - path to directory with page dump. """ page = WM.Page.query.get(app_config_id=self.wiki.config._id, title=page_title) for filename, path in self._attachments(page_dir): try: with open(path) as fp: page.attach(filename, fp, content_type=utils.guess_mime_type(filename)) except IOError, e: allura_base.log.error("Can't open file: %s" % str(e)) self.exit(2)
def save_image(cls, filename, fp, content_type=None, thumbnail_size=None, thumbnail_meta=None, square=False, save_original=False, original_meta=None): if content_type is None: content_type = utils.guess_mime_type(filename) if not content_type.lower() in SUPPORTED_BY_PIL: log.debug('Content type %s from file %s not supported', content_type, filename) return None, None try: image = PIL.Image.open(fp) except IOError as e: log.error('Error opening image %s %s', filename, e, exc_info=True) return None, None format = image.format if save_original: original_meta = original_meta or {} original = cls(filename=filename, content_type=content_type, **original_meta) with original.wfile() as fp_w: try: if 'transparency' in image.info: image.save(fp_w, format, transparency=image.info['transparency']) else: image.save(fp_w, format) except Exception as e: session(original).expunge(original) log.error('Error saving image %s %s', filename, e) return None, None else: original = None thumbnail = cls.save_thumbnail(filename, image, content_type, thumbnail_size, thumbnail_meta, square) return original, thumbnail
def save_image(cls, filename, fp, content_type=None, thumbnail_size=None, thumbnail_meta=None, square=False, save_original=False, original_meta=None): if content_type is None: content_type = utils.guess_mime_type(filename) if not content_type.lower() in SUPPORTED_BY_PIL: log.debug('Content type %s from file %s not supported', content_type, filename) return None, None try: image = PIL.Image.open(fp) except IOError as e: log.error('Error opening image %s %s', filename, e, exc_info=True) return None, None format = image.format if save_original: original_meta = original_meta or {} original = cls( filename=filename, content_type=content_type, **original_meta) with original.wfile() as fp_w: try: if 'transparency' in image.info: image.save(fp_w, format, transparency=image.info['transparency']) else: image.save(fp_w, format) except Exception as e: session(original).expunge(original) log.error('Error saving image %s %s', filename, e) return None, None else: original = None thumbnail = cls.save_thumbnail( filename, image, content_type, thumbnail_size, thumbnail_meta, square) return original, thumbnail
def __init__(self, url, filename=None): extractor = ProjectExtractor(None, url, parser=stringio_parser) self.url = url self.filename = filename or os.path.basename(urlparse(url).path) self.type = guess_mime_type(filename) self.file = extractor.page['data']
def create_project(pid, nbhd): M.session.artifact_orm_session._get().skip_mod_date = True data = loadjson(pid, pid + '.json') # pprint(data) log.info('Loading: %s %s %s' % (pid, data.data.title, data.data.path)) shortname = convert_project_shortname(data.data.path) project = M.Project.query.get( shortname=shortname, neighborhood_id=nbhd._id) if not project: private = (data.access_level == 'private') log.debug('Creating %s private=%s' % (shortname, private)) one_admin = [ u.userName for u in data.admins if u.status == 'Active'][0] project = nbhd.register_project(shortname, get_user(one_admin), project_name=data.data.title, private_project=private) project.notifications_disabled = True project.short_description = data.data.description project.last_updated = datetime.strptime( data.data.lastModifiedDate, '%Y-%m-%d %H:%M:%S') M.main_orm_session.flush(project) # TODO: push last_updated to gutenberg? # TODO: try to set createdDate? role_admin = M.ProjectRole.by_name('Admin', project) admin_usernames = set() for admin in data.admins: # FIXME: skip non-active users if admin.userName in skip_perms_usernames: continue admin_usernames.add(admin.userName) user = get_user(admin.userName) c.user = user pr = M.ProjectRole.by_user(user, project=project, upsert=True) pr.roles = [role_admin._id] ThreadLocalORMSession.flush_all() role_developer = M.ProjectRole.by_name('Developer', project) for member in data.members: # FIXME: skip non-active users if member.userName in skip_perms_usernames: continue if member.userName in admin_usernames: continue user = get_user(member.userName) pr = M.ProjectRole.by_user(user, project=project, upsert=True) pr.roles = [role_developer._id] ThreadLocalORMSession.flush_all() project.labels = [cat.path.split('projects/categorization.root.')[1] for cat in data.categories] icon_file = 'emsignia-MOBILITY-red.png' if 'nsn' in project.labels or 'msi' in project.labels: icon_file = 'emsignia-SOLUTIONS-blue.gif' if project.icon: M.ProjectFile.remove(dict(project_id=project._id, category='icon')) with open(os.path.join('..', 'scripts', icon_file)) as fp: M.ProjectFile.save_image( icon_file, fp, content_type=utils.guess_mime_type(icon_file), square=True, thumbnail_size=(48, 48), thumbnail_meta=dict(project_id=project._id, category='icon')) ThreadLocalORMSession.flush_all() dirs = os.listdir(os.path.join(options.output_dir, pid)) frs_mapping = loadjson(pid, 'frs_mapping.json') if not options.skip_wiki and 'wiki' in dirs: import_wiki(project, pid, nbhd) if not options.skip_frs_download and not project.app_instance('downloads'): project.install_app('Downloads', 'downloads') if 'forum' in dirs: import_discussion(project, pid, frs_mapping, shortname, nbhd) if 'news' in dirs: import_news(project, pid, frs_mapping, shortname, nbhd) project.notifications_disabled = False ThreadLocalORMSession.flush_all() return project
def __init__(self, **kw): super(File, self).__init__(**kw) if self.content_type is None: self.content_type = utils.guess_mime_type(self.filename)