I have an axis camera which POSTS data to an address I give it. I would like it to post data to a rails application, but I haven’t found any examples of how to do that. The only information I have to work with is the header for the post request. I pulled a few things out, but here’s what I got:
REQUEST_METHOD = POST
QUERY_STRING =
CONTENT_TYPE = image/jpeg
DOCUMENT_ROOT = /var/www/staging/public
REQUEST_URI = /camera/1/images?
SCRIPT_NAME =
PATH_INFO = /camera/1/images
HTTP_CONTENT_DISPOSITION = attachment; filename="image09-12-16_21-16-28-90.jpg"
_ = _
As far as I can tell, The request itself contains the JPEG Image.
Axis publishes the following as a CGI script to save the images. It works, but I want it to be a part of my Rails app:
#!/usr/bin/perl -w
my $upload_dir = "/var/www/cgi-bin/upload/";
print "Content-Type: text/plain\r\n\r\n";
print "Upload OK\r\n";
my $file_name = $ENV{'HTTP_CONTENT_DISPOSITION'};
$file_name =~ s/^attachment; filename=\"(.*)\"$/$1/;
open (IMG_FILE, "> $upload_dir$file_name") or die "can't open >$upload_dir$file_name";
binmode(IMG_FILE);
while (<STDIN>) {
print IMG_FILE $_;
}
chmod 0666, "$upload_dir$file_name";
exit 0;
So the filename is in ENV{‘HTTP_CONTENT_DISPOSITION’}, which is clear in both the headers and the sample code. It seems, from the perl script, that the image data is all contained in STDIN. I have tried the following in rails:
imagefile = File.new("test_file.jpg","w+")
imagefile << STDIN.read
imagefile.close
but it did not work. The file test_file.jpg was empty.
Is there anything I am mission? Does anyone know how to save the request data as a file?
Does anyone know of any good libraries that allow http upload from Axis webcams?
Rails does not use a CGI interface with stdin/stdout etc. The body of a submitted request can be found in
request.body, so in the above changeSTDIN.readtorequest.body.readand you should be fine.