def get_contents(side): if side not in ('a', 'b'): return error('invalid side', 'Side must be "a" or "b", got %s' % side) # TODO: switch to index? might be simpler path = request.form.get('path', '') if not path: return error('incomplete', 'Incomplete request (need path)') idx = diff.find_diff_index(DIFF, side, path) if idx is None: return error('not found', 'Invalid path on side %s: %s' % (side, path)) d = DIFF[idx] abs_path = d.a_path if side == 'a' else d.b_path try: if is_binary(abs_path): size = os.path.getsize(abs_path) contents = "Binary file (%d bytes)" % size else: contents = open(abs_path).read() return Response(contents, mimetype='text/plain') except Exception: return error('read-error', 'Unable to read %s' % abs_path)
def get_image(side, path): if side not in ('a', 'b'): return error('invalid side', 'Side must be "a" or "b", got %s' % side) # TODO: switch to index? might be simpler if not path: return error('incomplete', 'Incomplete request (need path)') mime_type, enc = mimetypes.guess_type(path) if not mime_type or not mime_type.startswith('image/') or enc is not None: return error('wrongtype', 'Requested file of type (%s, %s) as image' % ( mime_type, enc)) idx = diff.find_diff_index(DIFF, side, path) if idx is None: return error('not found', 'Invalid path on side %s: %s' % (side, path)) d = DIFF[idx] abs_path = d.a_path if side == 'a' else d.b_path try: contents = open(abs_path).read() return Response(contents, mimetype=mime_type) except Exception: return error('read-error', 'Unable to read %s' % abs_path)
def get_contents(side): if side not in ('a', 'b'): return error('invalid side', 'Side must be "a" or "b", got %s' % side) # TODO: switch to index? might be simpler path = request.form.get('path', '') if not path: return error('incomplete', 'Incomplete request (need path)') idx = diff.find_diff_index(DIFF, side, path) if idx is None: return error('not found', 'Invalid path on side %s: %s' % (side, path)) d = DIFF[idx] abs_path = d.a_path if side == 'a' else d.b_path try: is_binary = util.is_binary_file(abs_path) if is_binary: size = os.path.getsize(abs_path) contents = "Binary file (%d bytes)" % size else: contents = open(abs_path).read() return Response(contents, mimetype='text/plain') except Exception: return error('read-error', 'Unable to read %s' % abs_path)
def get_image(side, path): if side not in ('a', 'b'): return error('invalid side', 'Side must be "a" or "b", got %s' % side) # TODO: switch to index? might be simpler if not path: return error('incomplete', 'Incomplete request (need path)') mime_type, enc = mimetypes.guess_type(path) if not mime_type or not mime_type.startswith('image/') or enc is not None: return error( 'wrongtype', 'Requested file of type (%s, %s) as image' % (mime_type, enc)) idx = diff.find_diff_index(DIFF, side, path) if idx is None: return error('not found', 'Invalid path on side %s: %s' % (side, path)) d = DIFF[idx] abs_path = d.a_path if side == 'a' else d.b_path try: contents = open(abs_path).read() return Response(contents, mimetype=mime_type) except Exception: return error('read-error', 'Unable to read %s' % abs_path)