Ejemplo n.º 1
0
def test_given_type_validation(jpgpath):
    mobj = MediaObject.fromobj(jpgpath.open('rb'),
                               url='foo.png',
                               type='images')
    assert mobj
    assert mobj.type == mobj.bucket == 'images'

    with pytest.raises(MediaValidationError):
        mobj = MediaObject.fromobj(jpgpath.open('rb'),
                                   url='foo.png',
                                   type='datasets')

    with pytest.raises(MediaValidationError):
        mobj = MediaObject.fromobj(jpgpath.open('rb'),
                                   url='foo.png',
                                   type='foobar')
Ejemplo n.º 2
0
def test_mobj_full_run(testidbmodel, jpgpath):
    url = "http://test.idigbio.org/idigbio_logo.jpg"
    mobj = MediaObject.fromobj(jpgpath.open('rb'), url=url)
    assert mobj.url == "http://test.idigbio.org/idigbio_logo.jpg"
    assert mobj.etag
    etag = mobj.etag
    mobj.derivatives = False
    mobj.insert_object(testidbmodel)
    mobj.last_status = 200
    mobj.last_check = datetime.now()
    mobj.modified = datetime.now()
    mobj.ensure_media(testidbmodel)
    mobj.ensure_media_object(testidbmodel)

    mfe = MediaObject.frometag(etag, idbmodel=testidbmodel)
    assert mfe
    for s in mobj.__slots__:
        if s != 'modified':
            assert (getattr(mobj, s) == getattr(mfe, s))

    mfu = MediaObject.fromurl(url, idbmodel=testidbmodel)
    assert mfu
    assert mfu.url == url
    for s in mobj.__slots__:
        if s != 'modified':
            assert (getattr(mobj, s) == getattr(mfu, s))
Ejemplo n.º 3
0
def test_mobj_bad_validation(pngpath):
    with pytest.raises(MediaValidationError):
        MediaObject.fromobj(pngpath.open('rb'), url='foo.png')

    with pytest.raises(MediaValidationError):
        MediaObject.fromobj(pngpath.open('rb'), url='foo.png', type="adsf")

    with pytest.raises(MediaValidationError):
        MediaObject.fromobj(pngpath.open('rb'), url='foo.png', type="images")
Ejemplo n.º 4
0
def upload_recordset(rsid, fname, idbmodel):
    filereference = "http://api.idigbio.org/v1/recordsets/" + rsid
    logger.debug("Starting Upload of %r", rsid)
    stor = IDigBioStorage()
    with open(fname, 'rb') as fobj:
        mo = MediaObject.fromobj(fobj,
                                 url=filereference,
                                 type='datasets',
                                 owner=config.IDB_UUID)
        k = mo.get_key(stor)
        if k.exists():
            logger.debug("ETAG %s already present in Storage.", mo.etag)
        else:
            mo.upload(stor, fobj)
            logger.debug("ETAG %s uploading from %r", mo.etag, fname)

        mo.ensure_media(idbmodel)
        mo.ensure_object(idbmodel)
        mo.ensure_media_object(idbmodel)
        logger.debug("Finished Upload of %r, etag = %s", rsid, mo.etag)
        return mo.etag
Ejemplo n.º 5
0
def test_mobj_fromobj_mp3(mp3path):
    mobj = MediaObject.fromobj(mp3path.open('rb'), url='foo.mp3')
    assert mobj.detected_mime == mobj.mime == 'audio/mpeg'
    assert mobj.etag == mp3path.computehash()
Ejemplo n.º 6
0
def test_mobj_fromobj_jpg(jpgpath):
    mobj = MediaObject.fromobj(jpgpath.open('rb'), url='foo.jpg')
    assert mobj.detected_mime == mobj.mime == 'image/jpeg'
    assert mobj.bucket == mobj.type == 'images'
    assert mobj.etag == jpgpath.computehash()
Ejemplo n.º 7
0
def upload():
    vals = {}
    j = request.get_json()
    if j is not None:
        vals.update(j)
    for k, v in request.values.iteritems():
        vals[k] = v

    filereference = vals.get("filereference")
    if not filereference:
        logger.warning("No filereference specified")
        return json_error(400, "Missing filereference")

    obj = request.files.get('file')
    etag = vals.get('etag')
    media_type = vals.get("media_type")
    mime = vals.get("mime")

    try:
        mime, media_type = validate_mime_for_type(mime, media_type)
    except MediaValidationError as mve:
        logger.warning("Bad mime/media_type combo: %r/%r", mime, media_type)
        return json_error(400, str(mve))

    r = MediaObject.fromurl(filereference, idbmodel=idbmodel)
    if r:
        logger.warning("Found existing object for %r", r.url)
        if r.owner != request.authorization.username:
            return json_error(403)

    if obj:
        # if either type or mime are null it will be ignored, if
        # present they change the behavior of fromobj
        try:
            mo = MediaObject.fromobj(obj, type=media_type, mime=mime, url=filereference, etag=etag)
        except MediaValidationError as mve:
            logger.warning("Validation failure, %r", mve)
            return json_error(400, str(mve))
        mo.upload(IDigBioStorage(), obj)
        mo.insert_object(idbmodel)
    elif etag:
        mo = MediaObject.frometag(etag, idbmodel)
        if not mo or not mo.get_key(IDigBioStorage()).exists():
            return json_error(404, "Unknown etag {0!r}".format(etag))

        mo.last_status = 200
        mo.last_check = datetime.now()
        mo.mime = mime or mo.detected_mime
        mo.type = media_type or mo.bucket

    else:
        mo = r or MediaObject()
        mo.last_status = None
        mo.last_check = None
        try:
            mo.mime, mo.type = validate_mime_for_type(mo.mime or mime, mo.type or media_type)
            if not (mo.mime and mo.type):
                logger.warning("Missing either mime(%r) or type(%r)", mo.mime, mo.type)
                return json_error(400, "Incomplete request")
        except MediaValidationError as mve:
            logger.warning("Validation Failure, %r", mve)
            return json_error(400, str(mve))

    mo.url = filereference
    mo.owner = request.authorization.username

    if r:
        mo.update_media(idbmodel)
    else:
        mo.insert_media(idbmodel)
    if mo.etag:
        mo.ensure_media_object(idbmodel)

    idbmodel.commit()
    return respond_to_record(mo, format='json')