initial git commit of imapi

This commit is contained in:
bluesaxman 2019-04-21 08:15:22 -06:00
commit a4cbf79f57
13 changed files with 325 additions and 0 deletions

151
imapi.pl Executable file
View File

@ -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 $_."<br />"; }
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 "")."</".($_[0] or "div").">\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(<FILE>) { print; }
close FILE;
}
sub giveBook {
my ($library, $book) = @_;
open(THEBOOK, $libraries_path.$library."/".$book) or return print "0";
while(<THEBOOK>) { 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"}';
}

15
libs/j/ImageListSelection Normal file
View File

@ -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;
});
}

3
libs/j/getGalleries Normal file
View File

@ -0,0 +1,3 @@
function getGalleries(api_url) {
getData(api_url+"?lg",putGalleries);
}

11
libs/j/getImage Normal file
View File

@ -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);
};
}

3
libs/j/getImageList Normal file
View File

@ -0,0 +1,3 @@
function getImageList(api_url, gallery) {
getData(api_url+"?gg!"+gallery,putImageList);
}

11
libs/j/imageViewElements Normal file
View File

@ -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");
}

47
libs/j/initGalery Normal file
View File

@ -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");
}

41
libs/j/initImage Normal file
View File

@ -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;
}

10
libs/j/listGalleries Normal file
View File

@ -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); };
});
}

4
libs/j/putGalleries Normal file
View File

@ -0,0 +1,4 @@
function putGalleries(data) {
window.galleriesList = JSON.parse(data).galeries;
listGalleries();
}

5
libs/j/putImageList Normal file
View File

@ -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);
}

9
libs/j/refreshImage Normal file
View File

@ -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;
}

15
libs/j/showImage Normal file
View File

@ -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;
}