Example #1
0
def update_file_gridfs(filepath=None, metadata=None, db_name=None, **kwargs):
    import os, mongo_gridfs_insert_file
    db, fs = mongo_gridfs_insert_file.connect_gridfs_mongodb(db_name=db_name)
    try:
        filename = os.path.basename(filepath)
        ext = filename.split('.')[-1].lower()
        if not kwargs.get('content_type'):
            if ext == 'jpg' or ext == 'jpeg':
                content_type = 'image/jpeg'
            elif ext == 'tif' or ext == 'tiff':
                content_type= 'image/tiff'
            else:
                content_type= 'image/' + str(ext)
        else:
            content_type = kwargs.get('content_type')
        md5 = md5_checksummer(filepath)
        if not mongo_gridfs_insert_file.find_record_gridfs(key={'md5': md5}, db_name=db_name, collection_name='fs.files'):
            try:
                ## Actually do an insert to gridfs instead
                with fs.new_file(filename=filename, content_type=content_type, metadata=metadata) as fp:
                    with open(filepath) as filedata:
                        fp.write(filedata.read())
                return fp, db
            except IOError:
                print ' IO ERROR '
                return False, False
        else:
            # = mongo_gridfs_insert_file.find_record_gridfs(key={"filename": filename}, db_name=db_name, collection_name='fs.files')
            check, res = update_filerecord_pymongo(filepath=filepath,metadata=metadata,db_name=db_name, content_type=content_type)
            return check, res
    except OSError:
        print 'Failed ', filepath
Example #2
0
def update_filerecord_pymongo(db_name=None, collection_name=None, filename=None, filepath=None, metadata=None, colorstyle=None, alt=None, format=None, timestamp=None, **kwargs):
    import pymongo, bson
    from bson import Binary, Code
    from bson.json_util import dumps
    import datetime
    import mongo_gridfs_insert_file
    mongo_db, fs = mongo_gridfs_insert_file.connect_gridfs_mongodb(db_name=db_name)
    if fs:
        collection_name = 'fs.files'
        if not alt:
            alt = '1'

    tmpfilename          = str(filepath.split('/')[-1])
    colorstyle           = str(tmpfilename[:9])
    image_number         = str(tmpfilename.split('.')[-2][-1])
    alt                  = image_number
    if not kwargs.get('content_type'):
        content_type  = str(tmpfilename.split('.')[-1]).lower().replace('jpg', 'jpeg')
    else:
        content_type = kwargs.get('content_type')
    if not timestamp:
        timestamp = datetime.datetime.now()

    mongo_collection = mongo_db[collection_name]
    md5 = md5_checksummer(filepath)
    key = {'md5': md5}  #, 'alt': alt, 'upload_ct': 1}
    # data = { "$set":{'format': format,'metadata': metadata,'alt': alt, upload_ct: 1,'timestamp': timestamp}},
    datarow = {'colorstyle': colorstyle, 'format': format,'metadata': metadata,'alt': alt, 'upload_ct': "1",'timestamp': timestamp}
    key_str = key.keys()[0]
    restest = mongo_collection.distinct({key_str: md5})
    #print ' distinct Res Test --> ', restest
    check = mongo_collection.find({key_str: md5}).count()
    #check = mongo_collection.find({key_str: tmpfilename}).count()
    if check:
        
        data = { "$set":{
                        "colorstyle": colorstyle,
                        "alt": {"$min": {"alt": alt}},
                        "format": format,
                        "metadata": metadata,
                        "content_type": content_type,
                        #"upload_ct":
                        "$inc": {"upload_ct": "1"},
                        #"$inc": {"upload_ct": int(1)},
                        "timestamp": { "$max": {"timestamp": timestamp}}
                        }
                    }
        print 'REFRESH IT ', check, data
        return check, data

    else:

        data = { "$set":{'colorstyle': colorstyle, 'format': format, 'metadata': metadata, 'alt': alt, "$setOnInsert": {"upload_ct": 1},'timestamp': timestamp}}
        print 'NEW ', check, data
        # mongo_collection.ensure_index([("md5", pymongo.ASCENDING)], unique=True, sparse=True, background=True)
    try:
        mongo_collection.ensure_index(key_str, unique=True, background=True)
    except pymongo.errors.DuplicateKeyError:
        print ' DuplicateKey Error', key_str
        pass
    mongo_collection.create_index([("colorstyle", pymongo.DECENDING),("md5", pymongo.ASCENDING)], background=True)

    upsertobjid = mongo_collection.findAndModify(key, data, multi=True, safe=True, new=True)
    #upsertobjid = mongo_collection.update(key, data, upsert=True, multi=True, safe=True)
    print "Inserted: {0}\nImageNumber: {1}\nFormat: {2}\nID: {3}\nCheck: {4}".format(colorstyle,alt, format, upsertobjid, check)
    return check, upsertobjid