view restService/images.py @ 30:bcd8076ff7ec

random selection of entries bug fixes
author dwinter
date Wed, 05 Jun 2013 17:37:09 +0200
parents 7027fbf1d141
children a25bfc49a068
line wrap: on
line source

'''
handels the image path
Created on 13.03.2013

@author: dwinter
'''

import web
import managePurls.manageIndexMetaPURLs as manageIndexMetaPURLs
import logging
import urllib
import urllib2
import sunburnt
import os.path

#SOLR_URL="http://127.0.0.1:8983/solr/mpiwgSources"
SOLR_URL="http://md.mpiwg-berlin.mpg.de:8983/solr/collection1"
SCALERPATH="http://digilib.mpiwg-berlin.mpg.de/digitallibrary/servlet/Scaler?fn=%s"



class imageBasic:
    def __init__(self):
        self.md=manageIndexMetaPURLs.IndexMetaPURLManager()
        self.solr = sunburnt.SolrInterface(SOLR_URL)
        
    def getImagePathFromMetaData(self,purl):
        query=self.solr.query(**{'mpiwg-dri':purl})
        
        res = query.execute()
        
        if res.result.numFound == 0:
            return "",manageIndexMetaPURLs.NO_METADATA_FOUND
        
        doc=res.result.docs[0]

        path=doc.get('TT_image',None)
        
        
        if (path is not None) and (not isinstance(path, basestring)): #TT_image was  defined as multiple , shouldn't be the case ?
            path=path[0]
        
      
        archivePath=doc.get('archive-path',None)
       
        if (path == None) or (archivePath==None):
            return "",manageIndexMetaPURLs.NO_IMAGE_PATH_FOUND
        
        path = os.path.join(archivePath,path)
        path = path.replace("/mpiwg/online",'')
        return path,manageIndexMetaPURLs.VALID
        
    def getPath(self,purl):
        
        #first try check if path stored
        path,validity=self.md.getImagePathValidity(purl)
        
        if path=="":
           
            #second try gessing
            
            path,validity=self.getImagePathFromMetaData(purl)
           
            if path=="":
            
                path,validity=self.md.getPathValidity(purl)
                
                path=path.replace("index.meta","pageimg") #hope that images are stored at the required place
                 
                if path.startswith("http"): # there is a url stored, than this cannot be a path for the image server
                    raise web.notfound("NO IMAGE PATH STORED FOUND AN URL(%s)!"%path)
    


        if validity is manageIndexMetaPURLs.PERM_NON_VALID:
            raise web.notfound("PURL NON VALID ANYMORE!")
         
        if validity is manageIndexMetaPURLs.TEMP_NON_VALID:
            raise web.notfound("PURL currently not VALID try later!")
        
        
        data = web.input()
        
        if not 'dw' in data.keys():
            data['dw']="100"
       
        if not 'dh' in data.keys():
            data['dh']="100"
          
       
        return SCALERPATH%path+"&"+"&".join(["%s=%s"%(key,value) for key,value in data.items()])


class imageURL(imageBasic):
    def GET(self,purl):
        return self.getPath(purl)
        
class image(imageBasic):     
    def GET(self,purl):
        path = self.getPath(purl)
        print path
        raise web.redirect(path,"302 found")