Web service exposing images : ddl or base64? - image

My question is quite simple but I can't find any clear reference : I'm building a webservice that returns gameboard informations (in json for unity) and the image of the game.
Should I, in my informations include a field "image" with my image in base64 ?
Or in my information include a field "image" with the exposed url of the image (on my server still), and then do a second call to get the image ?
Which is the best practice toward unity android/ios ?

OK, i will prefer to use first option.Your information include image field.As http works with TCP it will deliver 100% and i think there is no need for second call to get game image

In my experience, use the second method avoid downloading the same image every time when I call the webservice.
I just check the image if exist in persistent data directory of my device. Don't need to download the resource again until the url content changed.

Related

Firebase functions callable - image as argument

I have a firebase function which given some arguments creates a "post" with a title and the such. Is there a way to send an image as an argument which can then be processed by firebase functions and then upload to firebase storage.
I think it is possible by decoding the file/image object to a base64 string which could be then sent as an argument. How would I convert the file object from html file input to base64? On the other side how would firebase functions know that the string is an actual image? How can I tell the size of the image? Is there a limit to the size of arguments given to a firebase callable function?
I ditched this question but came across the problem later in a different project. So to anyone seeing it is possible through base 64 strings. It has the restraints that the image cannot be bigger than 10mb but it is easier than trying to "catch" the file after it being uploaded directly.
Here is the solution
I would do the following:
Let the user upload the file directly into firebase storage in a folder where only the user has access to.
Let a function trigger on upload that takes this image, checks if the user is authorized to put that in the target folder and put the file in there (or whatever you want the function to do exactly).

Adding custom data based attributes based on Response object

Halo ! I'm trying to implement dropzonejs in a very specific way. Actually I follow the standard implementation described on the official page. Everything works perfectly.
But I'm willing to attach the server's generated URI for each uploaded file directly when uploaded : when uploading it's creating a database entry with some stuff like a page uri with title etc. This mean that the server would return as a response the id of the database saved file in order to attach the href attribute with its value to the the element in front.
This is quite ok to do this when only one file is uploaded, but it becomes trickier when bulk uploading.
So maybe I didn't understand the documentation well (and I'm quite sure I didn't), but is there any way to add custom data-dz-like attributes based on my server's response ? I'd like something like data-dz-url where the url points to a database entity (not the file itself).
Or if not if there is an "easy way" to handle this.
Thanks a lot
Here is the answer :
myDropzone.on('success', (file, response) => {
file.previewElement.href = "/admin/media/"+response.id+"/show/"
})
file is reference to the current uploaded element. It's possible to extend it's html attributes through previewElement. Setting the data-type attribute in the template before, then assigning it the right value works aswell.
Hope this will help some.

Google Custom Search API returning HTML documents instead of images

I started using the Google Custom Search API for a project, the idea is to search for images, and I wanted to use the Custom Search because the Google Images API is deprecated.
I already enabled image search on the CSE console
My query is like this:
https://www.googleapis.com/customsearch/v1?key=APIKEY&cx=CSECX&q=flower&alt=json&searchType=image&num=1&start=NUMBER
Where NUMBER is a random value between 1 and 20
Sometimes, it returns results like this:
{u'kind': u'customsearch#result', u'title': u'Flower Wallpaper Tumblr #6790199', u'displayLink': u'7-themes.com', u'htmlTitle': u'<b>Flower</b> Wallpaper Tumblr #6790199', u'snippet': u'Flower Wallpaper Tumblr', u'htmlSnippet': u'<b>Flower</b> Wallpaper Tumblr', u'link': u'http://7-themes.com/data_images/out/7/6790199-flower-wallpaper-tumblr.jpg', u'mime': u'image/jpeg', u'image': {u'thumbnailWidth': 150, u'byteSize': 808360, u'height': 1200, u'width': 1920, u'contextLink': u'http://7-themes.com/6790199-flower-wallpaper-tumblr.html', u'thumbnailLink': u'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSad0z_Wla0nRHAcQrjO5jLQkFjcoqnNHhejjuGmdA1AW2BqIVEpLARAk0s', u'thumbnailHeight': 94}}
Highlighting the interesting part:
u'link': u'http://7-themes.com/data_images/out/7/6790199-flower-wallpaper-tumblr.jpg', u'mime': u'image/jpeg'
So it seems that the URL is http://7-themes.com/data_images/out/7/6790199-flower-wallpaper-tumblr.jpg and mimetype is image/jpeg, but if you go to the URL, you'll see it's not an image, but an HTML document
Of course, I could capture this as an exception, but I don't want to waste daily API requests (out of a 100 limit per day) because the API didn't give me an image when I explicitly said so.
So, the question is: Is this normal behaviour, or misconfiguration/misuse on my part? If so, how could I fix it?
Thanks for your attention
After a little bit of reading, my best guess is that some servers are doing a resource redirect to prevent external sources from hotlinking directly to a resource. The file in question is advertised as an image, but accessing it from an external server will provide an HTML document instead. This is not a URL redirect, so it isn't detected by clients (including the Google crawler) until the resource is downloaded.
This sort of resource redirect is done on Apache servers using the .htaccess file and the RewriteEngine, with a technique similar to the one described here, although that particular technique can't be used to bait-and-switch images for HTML documents.
In short, if a server is lying about what type of file it's hosting, Google can't do anything about that. You can confirm that this is not an issue with the custom search API by performing the same query on the normal web search interface -- notice that clicking the image loads an HTML document rather than the image itself.

JSP to insert image into DB, display it to client

I want to insert an image to database and display it in another page. I am using the PostgreSQL database.
My guide suggests that I insert the image with its file path in the database. When displaying, in place of the src attrib of img tag put the path from database. So can I get any help for this .
Please guide me for this or give me link for similar kind of problem.
(I'm a final year student, and feel that this project requirement is difficult).
Your guide is entirely correct. Part of what you are supposed to be learning is problem solving: how to break a big problem down into many smaller, simpler problems you can solve piece by piece. It sounds like it's hinting at this, but expects you to be able to do that yourself, which is pretty reasonable.
You need to break this down into steps, and do each step in isolation. That's how anything but the most trivial programming task must be done.
(It isn't clear if you want to store the image data in the DB, or just a file system path, by the way, so I'm assuming you want to write the file to the local file system and just store the path in the DB).
Anyway, this should be fairly simple JSP. To display:
One JSP that:
Examines the query parameters for the image ID
Uses JDBC to fetch the associated path of the image on the file system from the database (a simple SELECT using the image ID as a query parameter)
Opens the image on the file system as a binary stream; also stats it to get its size
Sends appropriate HTTP headers eg Content-Type: image/jpeg and Content-Length: image-length-in-bytes to the client
Copies the raw image data from the image input stream to the output stream that sends to the client
Another JSP that generates the HTML and has an <img src="/the/image/jsp?imageid=blah"> link in it.
If you're required to submit just one JSP file, you can combine the two by having the JSP show a HTML page if it doesn't receive any query parameters, and send an image if it does receive an image id as a query parameter.
To insert:
One JSP that displays a HTML form with a file upload link if it doesn't get called with any HTTP POST data
If the JSP does get called with HTTP POST data:
** Issue a JDBC INSERT to create a record for the file in the database, but do not commit
** Access and decode the POST data using the methods provided in JSP
** Extract the desired file name from the form data and open a binary output stream to a file on the filesystem with that name
** Copy the image bytes into that output stream, url-decoding if required (the HTTP POST form handling code in JSP is likely to decode it to a byte stream for you, though)
** Flush and close the output stream
** Commit the transaction with the JDBC INSERT.
You should be able to find numerous examples of both with a quick Google search. If you can't, adapting examples from other programming languages should be easy enough.
For inserting you must think carefully about the error cases. That's a large part of proper programming.
I am intentionally not showing you code examples. You should be able to do this yourself if you're a final year student. You won't know everything you need, but by now you should know how to find out what you don't know when you need to know it. Tutorials. Documentation. Google. Writing test programs to figure things out. Method name autocomplete in NetBeans / Eclipse. Adapting sample code. You've got lots of options.

How to retrieve an image from a database using imageid in the controller

Saving the image in a web directory and storing the URL in the database using this approach, I stored the image URL in the database. Based on that image id (I need to pass this image id to the controller from an Ajax call). I need to retrieve the image.
I got the image id using a jQuery template, so I have passed that image id to the controller. What should I write in the controller, filepathresult or fileresult? Or is there another approach?
OK, you're a bit confused.
You have the actual image file, file.jpg and you have the physical path to the file, D:\some\path\to\file.jpg.
You have the URL path to the file and a surrogate identity (your id).
id: 1337 (some random number)
URL: ????
First question:
You say you're storing the URL. Is it really the complete URL? Is it just a partial path to the image? Is the path from the root of the website or the root of the application? Is it just a partial physical path?
Second question:
What are you actually trying to do?
Do you just want to get the full path to the image? Why do you need Ajax to do this, if you already have the id? You might want to rethink how you're storing the images if any performance needs to come out of this.
Once an image gets a new identity, it often makes sense to use that new identity everywhere; you might ought to consider copy/rename the file for the new identity after it's uploaded (and possibly save the old filename for record keeping purposes). If you need to keep the file names (more or less) as-is, however, it'd be better to provide the ability to grab the URLs for a whole set of ids rather than to individually make an Ajax request id-by-id.
If you request a resource (AKA navigate to a URL) that has a physical file, IIS is going to serve it directly (that is, if you ask for www.mysite.com/Images/Image3.jpg IIS is going to serve it directly). I really don't understand exactly what you are doing, but if you mean that you get the associated URL for an image using an Ajax call to an MVC controller with the id of the image, you could do several things.
You could simply return the URL and use JavaScript code to create an image tag with that URL and inserting it in the DOM.
You could return a view like <img src="{yoururl}" /> and insert it in the DOM using JavaScript.
You could store the images on the database directly and use the File method to return the image bits indicating the correct MIME type.

Resources