def METHOD_NAME(cls, data, file_type=None, im=None, attributes=()):
    sha256 = hashlib.sha256(data).hexdigest()
    obj = (cls.query
           .filter(cls.sha256 == sha256)
           .first())
    if obj is None:
        attributes = dict(attributes)
        if file_type is None and im is not None:
            file_type = images.image_file_type(im)
        if file_type is None:
            raise ValueError('a file type is required')
        if im is not None:
            attributes.update({'width': im.size.width, 'height': im.size.height})
        elif file_type == 'swf':
            attributes.update(flash.parse_flash_header(BytesIO(data)))
        obj = cls(sha256=sha256, file_type=file_type, attributes=attributes)
        # Write our file to disk
        real_path = obj.full_file_path
        makedirs_exist_ok(os.path.dirname(real_path))
        with open(real_path, 'wb') as outfile:
            outfile.write(data)
        cls.dbsession.add(obj)
    return obj