commit a4cbf79f57a4f6e790a61c7a5b646e895137ae1b Author: Bluesaxman Date: Sun Apr 21 08:15:22 2019 -0600 initial git commit of imapi diff --git a/imapi.pl b/imapi.pl new file mode 100755 index 0000000..e1ffd57 --- /dev/null +++ b/imapi.pl @@ -0,0 +1,151 @@ +#!/usr/bin/perl -w + +use strict; +use warnings; +use JSON; +use Image::Magick; + +my $images_path = "/home/bluesaxman/imagemasterImages/"; +my $libraries_path = "/home/bluesaxman/labs/p/imagemaster/libs//"; +my $json = JSON->new; + + +sub get_info { +# foreach (%ENV) { print $_."
"; } + my $clientIP = ($ENV{"REMOTE_ADDR"} or "0.0.0.0"); + my $proto = ($ENV{"REQUEST_SCHEME"} or "http"); + my $query = (split(/\?/,($ENV{"QUERY_STRING"} or "")))[0]; + my $referrer = ($ENV{"HTTP_REFERER"} or ""); + return ($proto,$clientIP,$query,$referrer); +} + +sub http_status { + my ($status,$content,$target) = @_; + my $header = ""; + $header .= "status: ".$status."\r\n"; + $header .= "Location: ".$target."\r\n" if $target; + $header .= "Content-Type: ".$content."\r\n" if $content; + $header .= "\r\n"; + return $header; +} + +sub html_element { + return "<".($_[0] or "div").($_[2] or "").">".($_[1] or "")."\n"; +} + +sub style_links { + return html_link("icon","image/gif","img/".$_[0].".png").html_link("stylesheet","text/css","css/css_".$_[0]); +} + +sub page_content { + return html_element("html", html_element("head", html_element("title",shift).shift ).html_element("body",shift) ); +} + +sub soft_die { + print http_status(500,"text/html; charset=utf-8"); + print page_content("500",style_links("no"),shift); + exit; +} + +sub normalize_string { + return join(" ",(map { ucfirst($_) } split("_",shift))); +} + +sub list_galleries { + opendir(GALLERY,$images_path); + my @galleries = readdir(GALLERY); + close GALLERY; + my @filtered_galleries = (); + foreach (@galleries) { + next if ($_ =~ m/^\./); + next unless (-d $images_path.$_ ); + push(@filtered_galleries,$_); + } + return $json->encode(\@filtered_galleries); +} + +sub list_images { + my $path = $images_path."/".$_[0]; + opendir(GALLERY,$path); + my @images = readdir(GALLERY); + close GALLERY; + my @filtered_images = (); + foreach (@images) { + next if ($_ =~ m/^\./); + next if (-d $path."/".$_ ); + push(@filtered_images, $_); + } + return $json->encode(\@filtered_images); +} + +sub make_imagepath { + my ($name, $gallery, $version) = @_; + return $gallery."/".$version."/".$name; +} + +sub thumb_gen { + my ($image,$thumb)=@_; + my $file = Image::Magick->new; + $file->Read("$image"); + $file->Resize('200x200'); + print $file->Write("$thumb"); +} + +sub get_image { + print http_status(200,"image/png;"); + my ($name,$gallery,$version) = @_; + unless ($name) { $name = "test.jpg"; } + unless ($gallery) { $gallery = "/"; } + unless ($version) { $version = "/"; } + my $full_path = $images_path.make_imagepath($name,$gallery,""); + my $thumb_path = $images_path.make_imagepath($name,$gallery,$version); + if ( "thumb" eq $version ) { + unless (-e $thumb_path ) { + thumb_gen($full_path,$thumb_path); #If the thumb doesn't exsist we make it. + } + open(FILE,"<",$thumb_path) or return print "0"; + } else { + if ( "" eq $version ) { + open(FILE,"<",$full_path) or return print "0"; + } else { + open(FILE,"<",$thumb_path) or return print "0"; + } + } + while() { print; } + close FILE; +} + +sub giveBook { + my ($library, $book) = @_; + open(THEBOOK, $libraries_path.$library."/".$book) or return print "0"; + while() { print; } + close THEBOOK; +} + +sub clean_input { + my $input = shift; + if ($input =~ m!%2F!) { print "Location: /hax\r\n\r\n"; exit; } + $input =~ s!%(..)!chr hex $1!ge; + $input =~ s!\+! !g; + return $input; +} + +# And now for our verbs +my @request = get_info(); +my @get_params = split("!",clean_input($request[2])); +my $directive = shift(@get_params); +if ( "i" eq $directive ) { + get_image(@get_params); +} elsif ( "lg" eq $directive ) { + print http_status(200,"text/json"); + print '{"galeries":'.list_galleries().'}'; +} elsif ( "gg" eq $directive ) { + print http_status(200,"text/json"); + print '{"images":'.list_images($get_params[0]).'}'; +} elsif ( "l" eq $directive ) { + print http_status(200,"text/plain"); + giveBook(@get_params); +} else { + print http_status(200,"text/json"); + print '{"error":"No input or incorrect input"}'; +} diff --git a/libs/j/ImageListSelection b/libs/j/ImageListSelection new file mode 100644 index 0000000..0b19549 --- /dev/null +++ b/libs/j/ImageListSelection @@ -0,0 +1,15 @@ +function ImageListSelection(position) { + var selection = window.curGallery.imageList.slice(position,position+5); + window.curGallery.container.list.innerHTML = ""; + selection.forEach( function (e,i) { + var element = elementPlace("#ImageList",e,"thumbnail","li"); + element.innerHTML = e; + element.style.backgroundImage = "url("+window.api_url+"?i!"+e+"!"+window.curGallery.Name+"!thumb)"; + element.style.backgroundRepeat = "no-repeat"; + element.style.backgroundPosition = "center center"; + element.onclick = function () { + initImage(window.curGallery.Position+i,window.curGallery.Name); + }; + element.innerHTML = e; + }); +} diff --git a/libs/j/getGalleries b/libs/j/getGalleries new file mode 100644 index 0000000..1f74f0f --- /dev/null +++ b/libs/j/getGalleries @@ -0,0 +1,3 @@ +function getGalleries(api_url) { + getData(api_url+"?lg",putGalleries); +} diff --git a/libs/j/getImage b/libs/j/getImage new file mode 100644 index 0000000..8cb45d7 --- /dev/null +++ b/libs/j/getImage @@ -0,0 +1,11 @@ +function getImage(imageName, galleryName) { + var Image = elementPlace("body",imageName,null,"div"); + Image.className = "thumbnail"; + Image.innerHTML = imageName; + Image.style.backgroundImage = "url("+window.api_url+"?i!"+imageName+"!"+galleryName+"!thumb)"; + Image.style.backgroundRepeat = "no-repeat"; + Image.style.backgroundPosition = "center center"; + Image.onclick = function () { + showImage(imageName, galleryName); + }; +} diff --git a/libs/j/getImageList b/libs/j/getImageList new file mode 100644 index 0000000..3076abf --- /dev/null +++ b/libs/j/getImageList @@ -0,0 +1,3 @@ +function getImageList(api_url, gallery) { + getData(api_url+"?gg!"+gallery,putImageList); +} diff --git a/libs/j/imageViewElements b/libs/j/imageViewElements new file mode 100644 index 0000000..b4e0fdb --- /dev/null +++ b/libs/j/imageViewElements @@ -0,0 +1,11 @@ +function imageViewElements () { + window.curImage.back = elementPlace("body","ImageView_back",null,"div"); + window.curImage.container = elementPlace("#ImageView_back","ImageView",null,"div"); + window.curImage.controls = elementPlace("#ImageView",null,"Icontrols","div"); + window.curImage.controls.previous = elementPlace(".Icontrols",null,"previous button","div"); + window.curImage.controls.position = elementPlace(".Icontrols","position",null,"div"); + window.curImage.controls.next = elementPlace(".Icontrols", null, "next button", "div"); + window.curImage.controls.close = elementPlace(".Icontrols", null, "close button", "div"); + window.curImage.container.imagePlace = elementPlace("#ImageView","ImageContainer",null,"div"); + window.curImage.container.imagePlace.image = elementPlace("#ImageContainer",null,null,"img"); +} diff --git a/libs/j/initGalery b/libs/j/initGalery new file mode 100644 index 0000000..3eb61d2 --- /dev/null +++ b/libs/j/initGalery @@ -0,0 +1,47 @@ +function initGalery(galleryName) { + window.curGallery = {}; + getImageList(window.api_url, galleryName); + window.curGallery.Position = 0; + window.curGallery.Name = galleryName; + window.curGallery.back = elementPlace("body","ImageGallery_back",null,"div"); + window.curGallery.container = elementPlace("#ImageGallery_back","ImageGallery",null,"div"); + window.curGallery.controls = elementPlace("#ImageGallery",null,"controls","div"); + window.curGallery.controls.previous = elementPlace(".controls",null,"previous button","div"); + window.curGallery.controls.previous.innerHTML = "Previous"; + window.curGallery.controls.previous.onclick = function () { + window.curGallery.controls.previous.innerHTML = ""; + if (window.curGallery.Position < 10) { + window.curGallery.Position = 0; + } else { + window.curGallery.Position -= 5; + window.curGallery.controls.previous.innerHTML = "Previous"; + } + window.curGallery.controls.next.innerHTML = "Next"; + window.curGallery.controls.position.innerHTML = window.curGallery.Position + " - " + (window.curGallery.Position + 5) + " / " + window.curGallery.imageList.length; + ImageListSelection(window.curGallery.Position); + }; + window.curGallery.controls.position = elementPlace(".controls","position",null,"div"); + window.curGallery.controls.position.innerHTML = window.curGallery.Position + " - " + (window.curGallery.Position + 5); + window.curGallery.controls.next = elementPlace(".controls",null,"next button","div"); + window.curGallery.controls.next.innerHTML = "Next"; + window.curGallery.controls.next.onclick = function () { + window.curGallery.controls.next.innerHTML = ""; + if (window.curGallery.Position >= window.curGallery.imageList.length - 10) { + window.curGallery.Position = window.curGallery.imageList.length - 5; + } else { + window.curGallery.Position += 5; + window.curGallery.controls.next.innerHTML = "Next"; + } + window.curGallery.controls.previous.innerHTML = "Previous"; + window.curGallery.controls.position.innerHTML = window.curGallery.Position + " - " + (window.curGallery.Position + 5) + " / " + window.curGallery.imageList.length; + ImageListSelection(window.curGallery.Position); + }; + window.curGallery.controls.close = elementPlace(".controls",null,"close button","div"); + window.curGallery.controls.close.innerHTML = "Close"; + window.curGallery.controls.close.onclick = function () { + var node = window.curGallery.back; + node.parentNode.removeChild(node); + }; + window.curGallery.container.list = elementPlace("#ImageGallery","ImageList",null,"ul"); + +} diff --git a/libs/j/initImage b/libs/j/initImage new file mode 100644 index 0000000..e5ec503 --- /dev/null +++ b/libs/j/initImage @@ -0,0 +1,41 @@ +function initImage(imageIndex, galleryName) { + window.curImage = {}; + window.curImage.Position = imageIndex; + window.curImage.Name = window.curGallery.imageList[imageIndex]; + imageViewElements(); + window.curImage.controls.previous.innerHTML = "Previous"; + window.curImage.controls.previous.onclick = function () { + window.curImage.controls.previous.innerHTML = ""; + if (window.curImage.Position <= 1) { + window.curImage.Position = 0; + } else { + window.curImage.Position -= 1; + window.curImage.controls.previous.innerHTML = "Previous"; + } + refreshImage(); + window.curImage.controls.next.innerHTML = "Next"; + }; + window.curImage.controls.position.innerHTML = window.curImage.Name; + window.curImage.controls.next.innerHTML = "Next"; + window.curImage.controls.next.onclick = function () { + window.curImage.controls.next.innerHTML = "" + if (window.curImage.Position >= window.curGallery.imageList.length -2) { + window.curImage.Position = window.curGallery.imageList.length - 1; + } else { + window.curImage.Position += 1; + window.curImage.controls.next.innerHTML = "Next" + } + refreshImage(); + window.curImage.controls.previous.innerHTML = "Previous"; + }; + window.curImage.controls.close.innerHTML = "Close"; + window.curImage.controls.close.onclick = function () { + var node = window.curImage.back; + node.parentNode.removeChild(node); + }; + window.curImage.container.imagePlace.style.backgroundImage = "url("+window.api_url+"?i!"+window.curImage.Name+"!"+galleryName+"!thumb)"; + window.curImage.container.imagePlace.style.backgroundRepeat = "no-repeat"; + window.curImage.container.imagePlace.style.backgroundPosition = "center center"; + window.curImage.container.imagePlace.style.backgroundSize = "contain"; + window.curImage.container.imagePlace.image.src = window.api_url+"?i!"+window.curImage.Name+"!"+galleryName; +} diff --git a/libs/j/listGalleries b/libs/j/listGalleries new file mode 100644 index 0000000..ef6253a --- /dev/null +++ b/libs/j/listGalleries @@ -0,0 +1,10 @@ +function listGalleries() { + window.galleryBox = elementPlace("body","GalleryBox",null,"div"); + window.galleryList = elementPlace("#GalleryBox","GalleryList",null,"ul"); + window.galleryList.innerHTML = ""; + window.galleriesList.forEach(function(e) { + var gallery = elementPlace("#GalleryList",null,"Gallery","li"); + gallery.innerHTML = e; + gallery.onclick = function () { initGalery(e); }; + }); +} diff --git a/libs/j/putGalleries b/libs/j/putGalleries new file mode 100644 index 0000000..eee0320 --- /dev/null +++ b/libs/j/putGalleries @@ -0,0 +1,4 @@ +function putGalleries(data) { + window.galleriesList = JSON.parse(data).galeries; + listGalleries(); +} diff --git a/libs/j/putImageList b/libs/j/putImageList new file mode 100644 index 0000000..d3b0287 --- /dev/null +++ b/libs/j/putImageList @@ -0,0 +1,5 @@ +function putImageList(data) { + window.curGallery.imageList = JSON.parse(data).images; + window.curGallery.controls.position.innerHTML = window.curGallery.Position + " - " + (window.curGallery.Position + 5) + " / " + window.curGallery.imageList.length; + ImageListSelection(0); +} diff --git a/libs/j/refreshImage b/libs/j/refreshImage new file mode 100644 index 0000000..f6ecf0a --- /dev/null +++ b/libs/j/refreshImage @@ -0,0 +1,9 @@ + +function refreshImage() { + var galleryName = window.curGallery.Name; + window.curImage.Name = window.curGallery.imageList[window.curImage.Position]; + window.curImage.controls.position.innerHTML = window.curImage.Name; + window.curImage.container.imagePlace.style.backgroundImage = "url("+window.api_url+"?i!"+window.curImage.Name+"!"+galleryName+"!thumb)"; + window.curImage.container.imagePlace.image.src = ""; + window.curImage.container.imagePlace.image.src = window.api_url+"?i!"+window.curImage.Name+"!"+galleryName; +} diff --git a/libs/j/showImage b/libs/j/showImage new file mode 100644 index 0000000..8e28032 --- /dev/null +++ b/libs/j/showImage @@ -0,0 +1,15 @@ +function showImage(imageName, galleryName) { + window.curImage = {}; + imageViewElements(); + window.curImage.controls.position.innerHTML = imageName; + window.curImage.controls.close.innerHTML = "Close"; + window.curImage.controls.close.onclick = function () { + var node = window.curImage.back; + node.parentNode.removeChild(node); + }; + window.curImage.container.imagePlace.style.backgroundImage = "url("+window.api_url+"?i!"+imageName+"!"+galleryName+"!thumb)"; + window.curImage.container.imagePlace.style.backgroundRepeat = "no-repeat"; + window.curImage.container.imagePlace.style.backgroundPosition = "center center"; + window.curImage.container.imagePlace.style.backgroundSize = "contain"; + window.curImage.container.imagePlace.image.src = window.api_url+"?i!"+imageName+"!"+galleryName; +}