Example #1
0
def store_file(filename):
	if os.access(filename, os.R_OK):
		imsto = load_imsto(section)
		from _util import guess_mimetype
		ctype = guess_mimetype(filename)
		with open(filename) as fp:
			ret = imsto.store(fp, ctype, name=os.path.basename(filename))
			print ret
	else:
		print 'image {} not found or access deny'.format(filename)
Example #2
0
def store_file(filename):
    if os.access(filename, os.R_OK):
        imsto = load_imsto(section)
        from _util import guess_mimetype
        ctype = guess_mimetype(filename)
        with open(filename) as fp:
            ret = imsto.store(fp, ctype, name=os.path.basename(filename))
            print ret
    else:
        print 'image {} not found or access deny'.format(filename)
Example #3
0
    def store(self, file=None, content=None, ctype=None, **kwd):
        """save a file-like item"""
        if content is None and not hasattr(file, 'read'):
            raise TypeError('invalid file-like object')

        data = content if content is not None else file.read()
        size = len(data)
        ext = guessImageType(data[:32])
        if ext is None:
            raise ValueError('invalid image file')

        hashes = [md5(data).hexdigest()]
        _exists_id = self.exists(hashed=hashes[0])
        if _exists_id:
            id = _exists_id
            filename = _make_filename(id, ext)
            print('id {} or hash {} exists!!'.format(id, hashes[0]))
            #raise DuplicateError('already exists')
            return [True, id, filename]
        ids = [_make_id(hashes[0])]
        if 'id' in kwd and kwd['id'] and kwd['id'] not in ids:
            ids += [kwd['id']]

        from image import SimpImage, MIN_QUALITY

        max_file_size = int(self.get_config('max_file_size'))
        max_jpeg_quality = int(self.get_config('max_jpeg_quality'))
        max_width = int(self.get_config('max_width'))
        max_height = int(self.get_config('max_height'))

        if size > max_file_size: max_jpeg_quality -= 1
        if max_jpeg_quality < MIN_QUALITY: max_jpeg_quality = MIN_QUALITY

        im = SimpImage(blob=data)
        meta = im.meta
        if meta['width'] > max_width or meta['height'] > max_height:
            if self.get_config('auto_scale') and im.thumbnail(
                    max_width, max_height):
                if im.format == 'JPEG' and im.quality > max_jpeg_quality:
                    im.quality = max_jpeg_quality
                data = im.get_blob()
                size = len(data)
                print im.meta
                print 'new scaled size {}'.format(size)
                hashes += [md5(data).hexdigest()]
            else:
                raise ValueError(
                    'file: {} dimension {}x{} is too big, max is {}x{}'.format(
                        kwd['name'] if 'name' in kwd else '', meta['width'],
                        meta['height'], max_width, max_height))

        if im.format == 'JPEG':
            if im.quality > max_jpeg_quality:
                print 'quality {} is too high, hash {}'.format(
                    im.quality, hashes[0])
                from tempfile import NamedTemporaryFile
                _tmp = NamedTemporaryFile('w+b',
                                          dir=self.get_config('temp_root'),
                                          delete=False)
                _tmp.file.close()
                save_file(_tmp.name, blob=data)
                if jpegoptim(_tmp.name):
                    fp = open(_tmp.name)
                    data = fp.read()
                    size = len(data)

                    # print 'new optimized size {}'.format(size)
                    fp.close()
                    _tmp.unlink(_tmp.name)
                    del im
                    im = SimpImage(blob=data)
                    meta = im.meta
                    hashes += [md5(data).hexdigest()]
                else:
                    raise EnvironmentError(
                        'jpeg qualty is too high, or need jpegoptim')
        elif im.format == 'PNG' and self.get_config('force_jpeg'):
            im.format = 'JPEG'
            im.quality = max_jpeg_quality
            data = im.get_blob()
            size = len(data)
            hashes += [md5(data).hexdigest()]
            ext = 'jpg'
            meta = im.meta
        del im

        if (size > max_file_size):
            raise ValueError('file: {} size {} is too big, max is {}'.format(
                kwd['name'] if 'name' in kwd else '', size, max_file_size))

        hashed = hashes[len(hashes) - 1]  #md5(data).hexdigest()
        # print ('md5 hash: {}'.format(hashed))

        # TODO: add for support (md5 + size) id
        id = _make_id(hashed)

        # print ('new filename: %r' % filename)

        # TODO: fix for support s3 front browse
        _exists_id = self.exists(id) or self.exists(hashed=hashed)
        if _exists_id:
            id = _exists_id
            filename = _make_filename(id, ext)
            print('id {} or hash {} exists!!'.format(id, hashed))
            #raise DuplicateError('already exists')
            return [True, id, filename]
        filename = _make_filename(id, ext)
        # print ('id: {}'.format(id))

        # if ctype is None or ctype == '':
        from _util import guess_mimetype
        ctype = guess_mimetype(filename)

        # save to mongodb
        spec = {
            '_id': id,
            'filename': filename,
            'hash': hashes,
            'mime': ctype,
            'size': size,
            'meta': meta,
            'ids': ids
        }

        if 'name' in kwd and isinstance(kwd['name'], (str, unicode)):
            spec['name'] = kwd['name']

        for k in ['created', 'app_id']:
            if k in kwd and kwd[k]:
                spec[k] = kwd[k]

        if self._store_exists(id, filename=filename):
            self._save_meta(id, spec)
            return [True, id, filename]

        rr = self._put(data, **spec)
        if rr:
            return [True, rr, filename]
Example #4
0
	def store(self, file=None, content=None, ctype=None, **kwd):
		"""save a file-like item"""
		if content is None and not hasattr(file, 'read'):
			raise TypeError('invalid file-like object')

		data = content if content is not None else file.read()
		size = len(data)
		ext = guessImageType(data[:32])
		if ext is None:
			raise ValueError('invalid image file')

		hashes = [md5(data).hexdigest()]
		_exists_id = self.exists(hashed=hashes[0])
		if _exists_id:
			id = _exists_id
			filename = _make_filename(id, ext)
			print ('id {} or hash {} exists!!'.format(id, hashes[0]))
			#raise DuplicateError('already exists')
			return [True, id, filename]
		ids = [_make_id(hashes[0])]
		if 'id' in kwd and kwd['id'] and kwd['id'] not in ids:
			ids += [kwd['id']]

		from image import SimpImage, MIN_QUALITY

		max_file_size = int(self.get_config('max_file_size'))
		max_jpeg_quality = int(self.get_config('max_jpeg_quality'))
		max_width = int(self.get_config('max_width'))
		max_height = int(self.get_config('max_height'))

		if size > max_file_size: max_jpeg_quality -= 1
		if max_jpeg_quality < MIN_QUALITY: max_jpeg_quality = MIN_QUALITY

		im = SimpImage(blob=data)
		meta = im.meta
		if meta['width'] > max_width or meta['height'] > max_height:
			if self.get_config('auto_scale') and im.thumbnail(max_width, max_height):
				if im.format == 'JPEG' and im.quality > max_jpeg_quality:
					im.quality = max_jpeg_quality
				data = im.get_blob()
				size = len(data)
				print im.meta
				print 'new scaled size {}'.format(size)
				hashes += [md5(data).hexdigest()]
			else:
				raise ValueError('file: {} dimension {}x{} is too big, max is {}x{}'.format(kwd['name'] if 'name' in kwd else '', meta['width'], meta['height'], max_width, max_height))

		if im.format == 'JPEG':
			if im.quality > max_jpeg_quality:
				print 'quality {} is too high, hash {}'.format(im.quality, hashes[0])
				from tempfile import NamedTemporaryFile
				_tmp = NamedTemporaryFile('w+b',dir=self.get_config('temp_root'),delete=False)
				_tmp.file.close()
				save_file(_tmp.name, blob=data)
				if jpegoptim(_tmp.name):
					fp = open(_tmp.name)
					data = fp.read()
					size = len(data)

					# print 'new optimized size {}'.format(size)
					fp.close()
					_tmp.unlink(_tmp.name)
					del im
					im = SimpImage(blob=data)
					meta = im.meta
					hashes += [md5(data).hexdigest()]
				else:
					raise EnvironmentError('jpeg qualty is too high, or need jpegoptim')
		elif im.format == 'PNG' and self.get_config('force_jpeg'):
			im.format = 'JPEG'
			im.quality = max_jpeg_quality
			data = im.get_blob()
			size = len(data)
			hashes += [md5(data).hexdigest()]
			ext = 'jpg'
			meta = im.meta
		del im

		if (size > max_file_size):
			raise ValueError('file: {} size {} is too big, max is {}'.format(kwd['name'] if 'name' in kwd else '', size, max_file_size))

		hashed = hashes[len(hashes)-1] #md5(data).hexdigest()
		# print ('md5 hash: {}'.format(hashed))

		# TODO: add for support (md5 + size) id
		id = _make_id(hashed)

		# print ('new filename: %r' % filename)

		# TODO: fix for support s3 front browse
		_exists_id = self.exists(id) or self.exists(hashed=hashed)
		if _exists_id:
			id = _exists_id
			filename = _make_filename(id, ext)
			print ('id {} or hash {} exists!!'.format(id, hashed))
			#raise DuplicateError('already exists')
			return [True, id, filename]
		filename = _make_filename(id, ext)
		# print ('id: {}'.format(id))

		# if ctype is None or ctype == '':
		from _util import guess_mimetype
		ctype = guess_mimetype(filename)

		# save to mongodb
		spec = {'_id': id,'filename': filename, 'hash': hashes, 'mime': ctype, 'size': size, 'meta': meta, 'ids': ids}

		if 'name' in kwd and isinstance(kwd['name'], (str, unicode)):
			spec['name'] = kwd['name']

		for k in ['created', 'app_id']:
			if k in kwd and kwd[k]:
				spec[k] = kwd[k]

		if self._store_exists(id, filename=filename):
			self._save_meta(id, spec)
			return [True, id, filename]

		rr = self._put(data, **spec)
		if rr:
			return [True, rr, filename]