def getImageD(self): rs = getattr(self,"imageD",None) if rs: return rs imd = image.loadImageD(self.image,getattr(self,"pageStore",None)) self.imageD = imd return imd
def publish(self,pageStore=None): OBSOLETE vprint("PUBLISHING ",self.__dict__) alb = self.topic tpo = misc.topicOb(alb) imtopic = tpo.imageTopic() imdir = tpo.imageOwner + "/" + tpo.imageName vprint("PUBLISH ALBUM",self.topic," with IMAGE ",imtopic,"imdir",imdir) js = self.compute_json(False) self.publishJson(js) tp = self.topic imageD = image.loadImageD(imtopic,getattr(self,"pageStore",None)) pg = self.genPage(self.computeTitle(imageD)) self.publishHtml(pg) snapDs = self.snaps() cropids = [] newPubs = [] fls = [] for snap in snapDs: if snap["published"]: continue crid = snap["cropid"] cropids.append(crid) fls.append(imdir+"/snap/"+str(crid)+".jpg") fls.append(imdir+"/snapthumb/"+str(crid)+".jpg") newPubs.append(snap) for fl in fls: vprint("about to save "+fl) s3.s3SaveFile(fl,relativeTo="images",contentType="image/jpeg") dynamo.assertPublished(self) self.setSnapsPublished(newPubs,1)
def addAlbum(webin): raise Exception("Obsolete function addAlbum") """ HOW IS THIS DIFFERENT FROM NEWALUM? It is the one that is in use. Handles the api/addAlbum request """ raise exc cks = webin.checkSessionResponse() if cks: return cks sess = webin.session user = sess.user cob = json.loads(webin.content()) im = str(cob["image"]) # the topic name /image/whatever importSnaps = cob.get("importSnaps", None) albumD = album.AlbumD(None) albumD.image = im albumD.owner = user imD = image.loadImageD(im, webin.pageStore) if (not getattr(imD, "shared", False)) and (imD.owner != user): imD.shared = 1 imD.dynsave(False) caption = cob.get("caption", None) if caption == None: caption = "unnamed" description = cob.get("description", None) if description == None: description = "" albumD.caption = caption albumD.description = description albumD.dynsave() if importSnaps: sna = dynamo.getSnapsAlbumTopic(im, user) if sna: albumD.importSnaps(sna) return okResponse(albumD.__dict__)
def publish(self,includeImages=True,pageStore=None): #vprint("PUBLISHING ",self.__dict__) """ the snap images appear under the image directory (/imagowner/imagename). pageOnly means don't bother with generating json""" alb = self.album tpo = misc.topicOb(alb) imtopic = tpo.imageTopic() imdir = tpo.imageOwner + "/" + tpo.imageName vprint("PUBLISH SNAP",self.topic," with IMAGE ",imtopic,"imdir",imdir) js = self.compute_json(False) tp = self.topic #topicdir = "/topicd/" if constants.publishToS3Dev else "/topic/" s3path = constants.topicDir+tp+"/main.json" #the path where the page will finally end up s3.s3SetContents(s3path,contents=js,relativeTo="",contentType="application/json") self.genPage(True) if not includeImages: return imageD = image.loadImageD(imtopic,getattr(self,"pageStore",None)) """ public = getattr(imageD,"isPublic",None) if not public: imageD.isPublic = 1 imageD.dynsave(False) """ crid = self.cropid; fls = [imdir+"/snap/"+str(crid)+".jpg",imdir+"/snapthumb/"+str(crid)+".jpg"] for fl in fls: vprint("about to save "+fl) s3.s3SaveFile(fl,relativeTo="images",contentType="image/jpeg")
def newAlbum(webin): cks = webin.checkSessionResponse() #f cks: return okResponse("virtual") if cks: return cks sess = webin.session user = sess.user cob = json.loads(webin.content()) vprint("NEW ALBUM COB: ", str(cob)) im = cob.get("image", None) cap = cob.get("caption", 'Untitled') if (not im) or (not cap): return failResponse("missingArguments") # for a very unlikely case #ha = album.hasAlbums(im,user) #if 0 and ha: # return failResponse("hasAlbums") newa = album.newAlbum(im, user, cap) ntp = newa.topic imD = image.loadImageD(im, webin.pageStore) if (not getattr(imD, "shared", False)) and (imD.owner != user): imD.shared = 1 imD.dynsave(False) #js = newa.compute_json(wrapInStatus=True) #snapst.closeStore(webin.pageStore) exv = newa.externalize() #js = json.dumps(exv) return okResponse(exv)
def loadImage(u, nm, altCaption=None): tp = imageTopic(u, nm) vprint("loading ", tp) im = image.loadImageD(tp) if im == None: print "MISSING ", tp if altCaption != None: im.title = altCaption images[tp] = im.__dict__
def imageDeleteable(webin): cob = json.loads(webin.content()) topic = cob["topic"] imageD = image.loadImageD(topic, webin.pageStore) if not imageD: return failResponse("missing") rs = imageD.deleteable() return okResponse(int(rs))
def publishPage(self,compressJs=True): alb = self.topic tpo = misc.topicOb(alb) imtopic = tpo.imageTopic() imageD = image.loadImageD(imtopic,getattr(self,"pageStore",None)) ttl = self.computeTitle(imageD) if ttl: ttl += ", Zoomable and Annotated" pg = self.genPage(compressJs=compressJs,title=ttl) self.publishHtml(pg)
def addSnap(self,sn,pageStore=None,publishInAlbum=True,scaleCoverage=True): albumId = misc.pathLast(self.topic) im = self.image cropcnti = im + "/crops" topic = getattr(sn,"topic",None) cropid = getattr(sn,"cropid",None) newSnap = topic==None coverage = sn.coverage # assumed unscaled imsplit = im.split("/") imuname = imsplit[2] imname = imsplit[3] imd = image.loadImageD(im) imdir = imd.imDir() imdim = imd.dimensions imwd = imdim["x"] imht = imdim["y"] svcov = sn.coverage if scaleCoverage: sn.coverage = misc.scaleRectDict(svcov,1.0/imwd); newCrop = False newSnap = False if topic == None: newCrop = True newSnap = True else: if cropid==None: newCrop = True snapid = misc.pathLast(topic) vprint("EDITING SNAPID "+snapid) newSnap = False if newCrop: cropid = dynamo.bumpCount(cropcnti) sn.cropid = cropid vprint ("im",im,"imuname",imuname,"cropcnti",cropcnti,"cropid",cropid) if newSnap: sn = self.newSnap(sn) else: self.updateSnap(sn) #if not topic: # sn.topic = "/snap/" + imuname + "/" + imname + "/" + str(albumId) + "/" + str(snapid) if newCrop: tl = image.Tiling(imd,256,1); tl.createTiles([],kind="r",parent=None,recursive=False) self.genSnapImages(imd,tl,sn) #cap = getattr(albumD,"caption",None) sn.publish() if publishInAlbum: sn.publishInAlbum() sn.coverage = svcov # this is what is wanted externally #snaps.closeStore(webin.pageStore) return sn
def genSnapImages(albumTopic): albumD = model.album.loadAlbumD(albumTopic) o = pathLast(albumD.owner) imt = albumD.image imname = pathLast(imt) albumIdx = pathLast(albumTopic) imd = image.loadImageD(imt) allsnaps = albumD.snaps() #snaps.getSnaps(albumTopic) tl = image.Tiling(imd,256,1); tl.createTiles([],kind="r",parent=None,recursive=False) for sn in allsnaps: sno = models.SnapD(None) sno.__dict__.update(sn) albumD.genSnapImages(imd,tl,sno)
def deleteImage(webin): cks = webin.checkSessionResponse() if cks: return cks sess = webin.session user = sess.user cob = json.loads(webin.content()) Logr.log("api", "DELETE SNAP COB: " + str(cob)) topic = cob["topic"] imageD = image.loadImageD(topic, webin.pageStore) if not imageD: return failResponse("missing") if user != imageD.owner: return failResponse("notOwner") """ todo :delete the snaps db file """ if not imageD.deleteable(): return failResponse("notDeleteable") imageD.delete() return okResponse()
def editImage(webin): """ Handles the api/addSnap request; this is used for editing snaps too """ cks = webin.checkSessionResponse() if cks: return cks sess = webin.session user = sess.user uname = models.pathLast(user) # Logr.activeTags = ["dispatch","api"] #Logr.log("api",str(napi.__dict__)) cob = json.loads(webin.content()) Logr.log("api", "COB: " + str(cob)) imagetopic = cob["image"] albumTopic = cob.get("albumTopic", None) albumTitle = cob.get("albumTitle", "") albumDescription = cob.get("albumDescription", "") imD = image.loadImageD(imagetopic, webin.pageStore) Logr.log("api", "IMD " + str(imD.__dict__)) imowner = getattr(imD, "owner", None) Logr.log("api", "OWNER " + str(imowner)) if not imowner: imowner = getattr(imD, "user", None) imD.owner = imowner if user != imowner: return failResponse("notOwner") imD.title = cob["title"] imD.description = cob["description"] imD.author = cob.get("author", None) imD.year = cob.get("year", None) imD.externalLink = cob.get("externalLink", None) imD.isPublic = cob.get("isPublic", None) imD.license = cob.get("license", None) imD.tags = cob.get("tags", None) imD.dynsave() if albumTopic: albumD = album.loadAlbumD(albumTopic, webin.pageStore) albumD.caption = albumTitle albumD.description = albumDescription albumD.dynsave() return okResponse({"title": imD.title, "description": imD.description})
def whenCanceled(job): #if jobsByTopic[tp]: del jobsByTopic[tp] imtopic = jobImtopic(job) vprint("whenCANCELED JOB", job.topic, "for image ", imtopic, job.kind) im = image.loadImageD(imtopic) if im == None: im = image.ImageD(None) im.owner = "/user/" + job.owner im.name = jobImname(job) imdir = im.imDir() imfile = imdir + "import" vprint("CANCELED, deleting ", imfile) if os.path.exists(imfile): os.remove(imfile) else: vprint("CANCELED deleting ", imtopic) if job.kind == "to_s3": vprint("CANCELED deleteFromS3", imtopic) im.deleteFromS3() im.delete()
def drawRect(self): area = 250000 cv = self.coverage cvr = image.dictToRect(cv) alb = self.album tpo = misc.topicOb(alb) imt = tpo.imageTopic() im = image.loadImageD(imt) dm = im.dimensions # determine scaling to the 250000 area ua = dm["x"] * dm["y"] ssq = ua/float(area) sc = math.sqrt(ssq) isc = 1/sc wx = isc * dm["x"] # width of the 250000 area rect = cvr.scale(wx) tp = self.topic snapnum = int(misc.pathLast(tp)) im.drawRect(snapnum,rect,area) return im
def allocateJob(webin): cks = webin.checkSessionResponse() if cks: return cks session = webin.session Logr.log("upload", "SESSION: " + str(session.__dict__)) user = session.user.split("/")[-1] cobs = json.loads(webin.content()) rs = [] vprint("allocateJob") for cob in cobs: knd = cob["kind"] jb = models.Job() jb.pageStore = webin.pageStore sb = cob["subject"] jb.kind = knd jb.owner = cob["owner"] jb.total = 0 jb.subject = sb vprint("KINDIS " + knd) if (knd == "upload") or (knd == "retrieve"): sbo = json.loads(sb) imname = sbo["image_name"] imtopic = "/image/" + user + "/" + imname im = image.loadImageD(imtopic, webin.pageStore) vprint("CHECKING EXISTS", imtopic) if im: vprint("YES EXISTS", imtopic) jb.status = "exists" jb.save() rs = "exists" break jb.save() vprint(" ALLOCATED JOB ", jb.topic) jobs.closeStore(webin.pageStore) rs.append(jb.__dict__) #upload = models.newUpload(nm,user,exists) return okResponse(rs)
def emitJson(webin): itm = time.time() sess = getattr(webin, "session", None) user = None if sess: user = sess.user jsonMimeType = "application/json" #jsonMimeType = "text/plain"; jsonMissing = json.dumps({"status": "error", "id": "missing"}) path = webin.path parsedPath = parseApiPath(webin.path) name = parsedPath.name category = parsedPath.category owner = parsedPath.owner vprint("category", category) if category == "image": imageTopic = "/image/" + owner + "/" + name #albumTopic = "/album/"+imowner+"/"+imname+"/"+album; Logr.log("api", "imageTopic=" + imageTopic) im = image.loadImageD(imageTopic, webin.pageStore) if not im: #todo add error page here return WebResponse('200 OK', jsonMimeType, jsonMissing) #Logr.log("api","IMAGE "+str(im.__dict__)) #print "IMAGE",im.__dict__ imob = {} improps = [ "topic", "externalLink", "dimensions", "author", "title", "owner", "tags", "description", "name", "year", "tilingDepth", "license", "shared", "beenTiled", "atS3", "current_item_create_time", "source", "isPublic", "s3Storage" ] misc.setDictPropsFromObject(imob, im, improps) #imob = {"id":im.topic,"dimensions":im.dimensions,"title":getattr(im,"title",""),"owner":"/user/"+owner, # "author":getattr(im,"author",""),"source":getattr(im,"source",""),"externalLink":getattr(im,"externalLink","")} #js = json.dumps(im.__dict__) #Logr.log("api","IMAGEJS"+js) js = json.dumps(imob) Logr.log("api", "IMAGEJS2" + js) return WebResponse('200 OK', jsonMimeType, js) if category == "album": """ the album id "-" means the snaps album of the current user """ imname = getattr(parsedPath, "name", None) id = getattr(parsedPath, "id", None) if not id: js = json.dumps({"status": "error", "id": "bad_path"}) return WebResponse('200 OK', jsonMimeType, js) albumTopic = "/album/" + owner + "/" + imname + "/" + id imTopic = "/image/" + owner + "/" + imname #ofl = open("/mnt/ebs0/imagediverdev/static"+albumTopic+"/topic.json","w") if id == "-": # the snaps album, if any if not user: return failResponse("missing") sna = dynamo.getSnapsAlbumTopic(imTopic, user) if sna: albumTopic = sna else: return failResponse("missing") albumD = album.loadAlbumD(albumTopic, webin.pageStore) js = albumD.compute_json(wrapInStatus=True) #snapst.closeStore(webin.pageStore) return WebResponse('200 OK', jsonMimeType, js) if category == "snap": imname = getattr(parsedPath, "name", None) id = getattr(parsedPath, "id", None) subid = getattr(parsedPath, "subid", None) if (not id) or (not subid): js = json.dumps({"status": "error", "id": "bad_path"}) return WebResponse('200 OK', jsonMimeType, js) snapTopic = "/snap/" + owner + "/" + imname + "/" + id + "/" + subid #ofl = open("/mnt/ebs0/imagediverdev/static"+albumTopic+"/topic.json","w") snapD = snapm.loadSnapD(snapTopic, webin.pageStore) js = snapD.compute_json(wrapInStatus=True) #snapst.closeStore(webin.pageStore) return WebResponse('200 OK', jsonMimeType, js) OBSOLETE() #ds = dstore.DStore(constants.descriptorStore) #imds = ds.descriptor(topicpath,'/type/imageD') snaps = models.snapsInAlbum(albumTopic) #theStore.topicsWithPropertyValue('/type/snapD','album',albumTopic) albums = models.albumsForImage(imageTopic) Logr.log("image", "snaps: " + str(snaps)) Logr.log("image", "image: " + str(im.__dict__)) Logr.log("image", "albums: " + str(albums)) snapDs = theStore.descriptor(snaps, '/type/snapD') albumDs = theStore.descriptor(albums, '/type/albumD') Logr.log("image", "albumDs" + str(albumDs)) thisAlbumD = None for albumD in albumDs: if albumD["topic"] == albumTopic: thisAlbumD = albumD break if im: otxt = pg0 + \ "<script>" + \ "var imD="+json.dumps(im.__dict__)+";\n" + \ "var snapDs="+json.dumps(snapDs)+";\n" + \ "var albumDs="+json.dumps(albumDs)+";\n"+ \ "var albumD="+json.dumps(thisAlbumD)+";\n"+ \ "var loggedInUser='******';\n" if forHomePage: otxt += "page.thisIsHomePage=1;\n" otxt += pg1 else: otxt = "NO SUCH IMAGE for " + topicpath snapst.closeStore(webin.pageStore) #Logr.log("image","description: "+str(imds)) return WebResponse('200 OK', 'text/html', otxt)
def imageForJob(job): topic = jobImtopic(job) return image.loadImageD(topic, getattr(job, "pageStore", None))
import api.job ajob = api.job import model.utilization import ops.logs logs = ops.logs import store.log logstore = store.log jt = "/job/test22/554" jb = models.getJob(jt) uu = models.UserD("/user/test22") uu.utilization() im = image.loadImageD("/image/test22/sistine77") """ sz = im.size() rct = image.Rect(image.Point(0,0),im.size()) ofl = "/mnt/ebs1/imagediver/images/test22/sistine25/tiling/r.jpg" ofl= "tiling/r.jpg" im.cropImage(rct,ofl,targetWidth=sz.x) ifl = "/mnt/ebs1/imagediver/images/test22/sistine25/stepdown/s_0.jpg" pim = image.openImageFile(ifl) ofl = "/mnt/ebs1/imagediver/images/test22/sistine25/tiling/r.jpg" pim.save(ofl,"JPEG",quality=95)
def emitAlbumPage(webin, parsedPath, parsedQuery, forHomePage): pageStore = {} import model.album album = model.album vprint("parsedQuery " + str(parsedQuery)) vprint("emitAlbumPage") sess = webin.session if sess == None: user = "" else: user = sess.user qs = getattr(webin, "queryString", None) published = 0 # is this the published version? """ now, if we're here its unpublished if qs: qsp = urlparse.parse_qs(qs) published = 0 if qsp.get('unpublished',False) else 1 """ name = parsedPath.name Logr.log("newdb", str(name)) if len(name) < 3: return gen.emitNotFound(webin) #return gen.genStaticPage(webin,"ImageDiver Message"," <center><b>404</b></center><p><center>No such page</center></p>"); imowner = name[0] imname = name[1] albumname = name[2] albumTopic = "/album/" + imowner + "/" + imname + "/" + albumname imageTopic = "/image/" + imowner + "/" + imname if albumname == "-": albumD = "-" else: albumD = album.loadAlbumD(albumTopic, webin.pageStore) if not albumD: #todo add error page here return gen.genStaticPage(webin, "ImageDiver Message", "<center><b>No such album</b></center>") im = image.loadImageD(imageTopic, webin.pageStore) imHasAlbums = album.hasAlbums( imageTopic, user) # does this user have albums on this image imDict = models.toDict(im, [ "dimensions", "title", "name", "author", "year", "externalLink", "description", "owner", "topic", "tilingDepthBump", "zoomDepthBump", "source" ]) if albumD == "-": ownerD = models.loadUserD(user, webin.pageStore) ownerName = getattr(ownerD, "name", None) options = { "imageD": imDict, "imageTopic": imageTopic, "loggedInUser": user, "ownerName": ownerName, "albumOwner": user, "published": False } otxt = genAlbumPage(options) return htmlResponse(otxt) #print "IMDICT ",imDict author = getattr(im, "author", None) if author == None: author = "" else: author = ", " + author ttl = getattr(im, "title", "") albowner = albumD.owner vprint("OWNER ", albowner, user) if (not constants.devVersion) and (albowner != user): return gen.genStaticPage( webin, "ImageDiver Message", "<center><b>This is an unpublished album</b></center>") beenPublished = getattr(albumD, "published", 0) vprint("beenPublished", beenPublished) ownerD = models.loadUserD(albowner, webin.pageStore) ownerName = getattr(ownerD, "name", None) apub = getattr(albumD, "published", None) if published and (not apub): return gen.genStaticPage( webin, "ImageDiver Message", "<center><b>This album has not yet been published</b></center>") #imageTopic = albumD.image #options = {"imageD":imDict,"albumOwner":albumD.owner,"albumOwnerName":ownerName,"loggedInUser":user,"hasAlbums":imHasAlbums, # "albumTopic":albumD.topic,"published":published,"beenPublished":beenPublished,"path":"/"} options = { "imageD": imDict, "albumOwner": albumD.owner, "albumOwnerName": ownerName, "loggedInUser": user, "imTitle": ttl, "imAuthor": author, "albumTopic": albumD.topic, "imageTopic": imageTopic, "published": published, "beenPublished": beenPublished, "path": webin.path } vprint("OPTIONS", options) otxt = genAlbumPage(options) #snaps.closeStore(webin.pageStore) return htmlResponse(otxt)
import store.jobs jobs = store.jobs import api.job ajob = api.job import model.utilization import ops.logs logs = ops.logs import store.log logstore = store.log import time import ops.imp imp = ops.imp from PIL import Image imd = image.loadImageD("/image/725f81a/big_ambassadors") tl = image.Tiling(imd,256,1); tl.createTiles([],kind="r",parent=None,recursive=False) rct = Rect(Point(4000,2000), Point(3000,1000)) rct = Rect(Point(4000,2000), Point(30,20)) ii = tl.findIntersectingTiles(rct,5,[]) print [t.id for t in ii] imdir = imd.imDir() ofl = imdir+"cfoob.jpg" afl = imdir+"afoob.jpg" #tl.assembleTiles(ii,ofl) tl.cropFromTiles(ofl,rct,targetWidth=500,targetHeight=None,targetArea=None,asmFile=afl) til = tl.tiles[0]