Exemple #1
0
def addKeyword( uuid ):
    key = request.GET.get('key', None)
    if ( key==None ):
        msg = 'addKeyword: "key" argument required but missing'
        log.error("/ws/addKeyword: " + msg)
        return { "uuid": uuid, "error": 1, "msg" : msg }
    key = helper.strfilter(key, "'/\. ")
    log.debug('addKeyword request for: %s with key: %s' % (uuid,`key`) )
    try:
        media = mediafactory.MediaFactory()
        media.loadFromFile(uuid)
        if media.customtags.has_key("keywords"):
            keys = media.customtags["keywords"]
            log.debug("Previous keywords " + `keys`)
            keys = keys + "," + key
            # `set' removes duplicate keywords
            keys = ','.join(set(keys.split(',')))
        else:
            keys = key
        media.update( { 'keywords' : keys }, group="Custom" )
        db.insert_key(key)
        db.connect_key(media.uuid, key)
    except ExifException as e:
        msg = str(e)
        log.debug("Exception: " + msg )
        return { "uuid":uuid, "error":1, "msg": msg }
    except Exception as e:
        return {"error":1 , "msg": str(e)}
    return { "uuid":uuid, "error":0, "msg": "Keyword saved!" }
Exemple #2
0
 def mkdir(self, path):
     """ Wrapper call around create_folder. Returns metadata. 
     If folder already exists we have to implement to "new file name" algorithm.        
     """
     path = self._addslash(path)
     #ban special characters for dirname
     dirname = helper.strfilter(path, "\\.~")
     #create dir if it doesn't exist
     realdir = self.realpath(dirname)
     if not os.path.exists(realdir):
         log.debug("creating dir: %s" % realdir)
         os.makedirs(realdir, 0770)
         return self.metadata(path)
     else:
         # File already exists. Find the next filename (XXX) available
         # by calling this function recursively. (Can be much faster
         # by first checking the results of ls before doing the recursion)
         numlist = re.findall(".* \(([0-9]*)\)$", path)
         if numlist:
             num = numlist[0]
             newpath = path[:path.rfind(num) - 2] + " (%d)" % (int(num) + 1)
         else:
             newpath = path + " (1)"
         log.debug("mkdir(): folder collisions. Trying new name: %s" %
                   newpath)
         return self.mkdir(newpath)
Exemple #3
0
def addKeyword(uuid):
    key = request.GET.get('key', None)
    if (key == None):
        msg = 'addKeyword: "key" argument required but missing'
        log.error("/ws/addKeyword: " + msg)
        return {"uuid": uuid, "error": 1, "msg": msg}
    key = helper.strfilter(key, "'/\. ")
    log.debug('addKeyword request for: %s with key: %s' % (uuid, ` key `))
    try:
        media = mediafactory.MediaFactory()
        media.loadFromFile(uuid)
        if media.customtags.has_key("keywords"):
            keys = media.customtags["keywords"]
            log.debug("Previous keywords " + ` keys `)
            keys = keys + "," + key
            # `set' removes duplicate keywords
            keys = ','.join(set(keys.split(',')))
        else:
            keys = key
        media.update({'keywords': keys}, group="Custom")
        db.insert_key(key)
        db.connect_key(media.uuid, key)
    except ExifException as e:
        msg = str(e)
        log.debug("Exception: " + msg)
        return {"uuid": uuid, "error": 1, "msg": msg}
    except Exception as e:
        return {"error": 1, "msg": str(e)}
    return {"uuid": uuid, "error": 0, "msg": "Keyword saved!"}
Exemple #4
0
 def mkdir(self, path):
     """ Wrapper call around create_folder. Returns metadata. 
     If folder already exists we have to implement to "new file name" algorithm.        
     """
     path = self._addslash(path)
     #ban special characters for dirname
     dirname = helper.strfilter( path ,"\\.~" )
     #create dir if it doesn't exist
     realdir = self.realpath(dirname)
     if not os.path.exists(realdir):
         log.debug("creating dir: %s" % realdir)
         os.makedirs(realdir, 0770)
         return self.metadata(path)
     else:
         # File already exists. Find the next filename (XXX) available 
         # by calling this function recursively. (Can be much faster
         # by first checking the results of ls before doing the recursion)
         numlist = re.findall(".* \(([0-9]*)\)$",path)
         if numlist:
             num = numlist[0]
             newpath = path[:path.rfind(num)-2] + " (%d)" % ( int(num) + 1 )
         else:
             newpath = path + " (1)"
         log.debug("mkdir(): folder collisions. Trying new name: %s" % newpath)
         return self.mkdir(newpath)
Exemple #5
0
    def put_file(self, path, fp, overwrite):
        """ Save file handler fp contents at path 
        Args:
            path (str) : chroot based path including filename
            fp (File) : file pointer pointing to data
            overwrite (bool): overwrite the file if it exists

        Returns:
            string: chrooted path that was actually used. This may be different than the 
                    original in case of existing file, illegal chars etc.

        Raises:
            error exception if the leaf directory already exists or cannot be created
        """
        # create dir if fullfile path has a dir
        # assume filename is everything after last `/'

        # TODO: make whitelisting instead of blacklisting filter!
        # TODO: WARNING: this needs serious auditing before release!

        # path should start with `/' otherwise add it!
        path = self._addslash(path)
        #ban special characters after last `/'
        dirname = helper.strfilter(path[:path.rfind("/")], "\\.~")
        filename = helper.strfilter(path[path.rfind("/"):], "~/")
        #create dir if it doesn't exist
        realdir = self.realpath(dirname)
        realfile = os.path.join(realdir, filename)
        if not os.path.exists(realdir):
            log.debug("creating dir: %s" % realdir)
            os.makedirs(realdir, 0770)
        # Check overwrite flag
        if not overwrite:
            if os.path.exists(realfile):
                return path  #do nothing
        log.debug("writing file: %s/%s" % (realdir, filename))
        with open(realdir + "/" + filename, "w") as f:
            f.write(fp.read())
        return path
Exemple #6
0
    def put_file(self, path, fp, overwrite):
        """ Save file handler fp contents at path 
        Args:
            path (str) : chroot based path including filename
            fp (File) : file pointer pointing to data
            overwrite (bool): overwrite the file if it exists

        Returns:
            string: chrooted path that was actually used. This may be different than the 
                    original in case of existing file, illegal chars etc.

        Raises:
            error exception if the leaf directory already exists or cannot be created
        """
        # create dir if fullfile path has a dir
        # assume filename is everything after last `/'

        # TODO: make whitelisting instead of blacklisting filter!
        # TODO: WARNING: this needs serious auditing before release!

        # path should start with `/' otherwise add it!
        path = self._addslash(path)
        #ban special characters after last `/'
        dirname = helper.strfilter( path[:path.rfind("/")] ,"\\.~" )
        filename = helper.strfilter( path[path.rfind("/"):], "~/" )                
        #create dir if it doesn't exist
        realdir = self.realpath(dirname)
        realfile = os.path.join(realdir, filename)
        if not os.path.exists(realdir):
            log.debug("creating dir: %s" % realdir)
            os.makedirs(realdir, 0770)
        # Check overwrite flag
        if not overwrite:
            if os.path.exists(realfile):
                return path #do nothing
        log.debug("writing file: %s/%s" % (realdir, filename))
        with open ( realdir + "/" + filename , "w" ) as f:
            f.write(fp.read())        
        return path
Exemple #7
0
 def loadFromURL(self, ip, url):
     """
     Create a media object sessions and also loads the metadata from the file based on a uploaded file.
     
     ip: remote address
     url: url of the file (string)
     """
     self.ip = ip
     self.uuid =  uuid.uuid4().hex
     # assuming filename is eveything after the rightmost `/'        
     self.filename = helper.strfilter( url[url.rfind('/'):] ,"\\/" )
     self.filedata = urllib.urlopen(url)
     self.filedir = os.path.join (configuration.getDataDir(), self.uuid)
     self.filefullpath = os.path.join (self.filedir, self.filename)
     self.ctime = time.strftime('%d%m%y-%H:%M:%S')
     self.customtags = {}
     self.mtime = self.ctime
     self._savefile()
     self.mediamd = helper.probedata(self.filefullpath)
     self.datasetname = helper.getBaseName(self.filename)
     db.insert_uuid(self.uuid)
     log.debug("filename: " + `self.filename`)
     log.debug("DatasetName (filename w/o extention): " + `self.datasetname`)