def wrapper(*args, **kwargs): if request.content_type != "image/jpeg": return ErrorResponseJson("invalid content type: {}".format( request.content_type)).make_response() if imghdr.test_jpeg(request.data, None) != "jpeg": return ErrorResponseJson("invalid jpeg data").make_response() return func(*args, **kwargs)
def _what_file(bytes): if imghdr.test_bmp(bytes, 0) == "bmp": return "bmp" elif imghdr.test_gif(bytes, 0) == "gif": return "gif" elif imghdr.test_jpeg(bytes, 0) == "jpeg": return "jpeg" elif imghdr.test_webp(bytes, 0) == "webp": return "webp" elif imghdr.test_png(bytes, 0) == 'png': return 'png'
def upload(): if request.content_type != 'image/jpeg': raise InvalidImage('Invalid content-type. Need image/jpeg') if not imghdr.test_jpeg(request.data, None): raise InvalidImage() try: result = process_image(request.data) image = index.neighbour(result[0]) if not image: return 'Internal server error during communication with index', 500 response = make_response(image) response.headers.set('Content-Type', 'image/jpeg') except Exception as error: print(error, file=sys.stderr) return 'Internal server error {}'.format(error), 500 return response
def ConvertDatToImage(srcDatFile, destDir): readStream = open(srcDatFile, 'rb') readBuf = readStream.read() buf6 = readBuf[0:4] hFEFE, nPicCount = struct.unpack("<HH", buf6) for iPic in range(0, nPicCount): buf9 = readBuf[4+9*iPic: 4+9*iPic+9] bPicInfo, iOffset, iSize = struct.unpack("<bii", buf9) bufIPic = readBuf[iOffset: iOffset+iSize] strExt = "" if(imghdr.test_png(bufIPic, None)): strExt = ".png" elif(imghdr.test_jpeg(bufIPic, None)): strExt = ".jpg" newJpgName = '''%s_%d%s''' % (os.path.splitext(os.path.split(srcDatFile)[1])[0], iPic, strExt) destFile = os.path.join(destDir, newJpgName) outFS = open(destFile, 'wb') outFS.write(bufIPic) outFS.close()
async def add_(self, name, url, author_id): try: await self.get(name) except EmoteNotFoundError: image_data = await self.fetch(url) if imghdr.test_gif(image_data, None) == 'gif': animated = True elif imghdr.test_png(image_data, None) == 'png' or imghdr.test_jpeg(image_data, None) == 'jpeg': animated = False else: raise InvalidImageError guild = self.free_guild(animated) emote = await guild.create_custom_emoji(name=name, image=image_data) await self.bot.db.execute( 'INSERT INTO emojis(name, id, author, animated) VALUES($1, $2, $3, $4)', name, emote.id, author_id, animated) return 'Emote %s successfully created.' % emote else: raise EmoteExistsError
def data_is_acceptable_img(data: str) -> bool: """Check if a given file data is a PNG or JPEG.""" return imghdr.test_png(data, None) or imghdr.test_jpeg(data, None)
# Rate limit if glob.redis.get("lets:screenshot:{}".format(userID)) is not None: return self.write("no") glob.redis.set("lets:screenshot:{}".format(userID), 1, 60) while os.path.exists( path := BASE_PATH.format(generalUtils.randomString(8))): pass # Check if the filesize is not ridiculous. Through my checking I # have discovered all screenshots on rosu are below 500kb. if sys.getsizeof(self.request.files["ss"][0]["body"]) > 500000: return self.write("filesize") # Check if the file contents are actually fine (stop them uploading eg videos). if (not test_jpeg(self.request.files["ss"][0]["body"], 0))\ and (not test_png(self.request.files["ss"][0]["body"], 0)): return self.write("unknownfiletype") # Write screenshot file to screenshots folder with open(path, "wb") as f: f.write(self.request.files["ss"][0]["body"]) # Output log.info("New screenshot ({})".format(path)) # Dirty method. ss_ting = path.removeprefix( glob.conf.config["server"]["screenshotspath"]) # Return screenshot link self.write("{}/ss{}".format(
def update_event(self, inp=-1): self.set_output_val(0, imghdr.test_jpeg(self.input(0), self.input(1)))