Forum OpenACS Development: Writting a blob to a file and displaying it

I was working on  " Potrait View " in ACS TCL. In the core, it uses db_write_blob to display a picture. But I saw some postings on openacs bboard that db_write_blob locks the database handle till the blob data is fetched from the database, written to a file and that file is displayed as an image. So if the internet speed is slow then it can cause problems.

The better solution is after getting the blob from the database, release the handle and then write to a file which can be displayed as an image.

I used db_0or1row to get the blob data in my tcl file and then open a file ( [open tmp_file w] ) in write mode. Then I put the blob data in the file using "puts" command. After the data is written to the file, I used ns_returnfile to display the file.

Here I am getting just a blank page. I checked ns_returnfile with a static image existing on my hard disk and it works fine. So the problem is while putting the blob data in the file.

Looking for some comments on it.

Collapse
Posted by Jeff Davis on
At a guess I would say you are probably getting messed up on
encodings.  Did you do a fconfigure -encoding binary on the
output channel?
Collapse
Posted by Anjani Kumar on
Hi Jeff,

Well, I have tried this too => fconfigure -encoding binary, I saw this has been used in one of the files in core. What I am thinking is that this looks a file writting problem with a Blob data. When I tried the same code for plain/text, it worked.

Thanks again and let me know if anything else comes up in your mind.

regards
Anjani

Collapse
Posted by Anjani Kumar on
Let me tell you what are the steps that I have done:

1. In the tcl file, I get the portarit in a variable (using db_0or1row).
2. I created a temporary file in /tmp directory (using [open $tmp_file w]).
3. I wrote the portrait to the file (using "puts $fd $portrait") and close the stream (close $fd).
4. Then I use ns_returnfile with the required parameters (ns_write 200 image/jpeg $tmp_file - using fixed mime type for testing).

But it is showing a blank page. Also I put $tmp_file in the SRC attribute of IMG tag in the adp file. Still didn't get the expected output. I have also tried with fpconfig.

waiting for some comments

Collapse
Posted by Dave Bauer on
Collapse
Posted by Anjani Kumar on
Well Dave the problem is not with db_write_blob: it works fine. The problem is I dont want to use this procedure as it locks the database handle till the blob data is fetched from the database, this slows the downloads.

Therefore The better solution is after getting the blob from the database, release the handle and then write to a file which can be displayed as an image. This will also work during slow connections. through ns_returnfile

How to do the above is a prob.

Collapse
Posted by Jeff Davis on
I think for portraits it does not matter if you hold the db handle or not, they are not generally large enough to be a real problem. Also you said "I put $tmp_file in the SRC attribute of IMG tag", which will not work since that will send a "GET $tmp_file" which will ask for a non-existent url on your server.

Have you tried checking whether the tmp file is corrupted or not?

Collapse
Posted by Anjani Kumar on
Thanks Jeff,

I wish I could get away with this problem. However i want to try my best before giving up.

I have tried to write to a file which is named "xxx.gif/xxx.jpg" and  put the exact path of this file in the src tag (adp file). But it also didn't help. I am tried almost every combination with mime types. It is only working fine with text/plain.

Actually, it does matter when you have an option to upload multiple portraits and if you can see them all together in your workspace. That is why I am tring this option.

Have you ever done this before? or anybody else?

Thanks again,
Anjani.

Collapse
Posted by Don Baccus on
Why not use db_blob_get_file to write the file?

Here's an example from acs-content-repository/tcl/publish-procs.tcl:

    db_blob_get_file wmb_get_blob_file "
      select content from cr_revisions where revision_id = $revision_id
    " -file $filename
Then you can use an image tag to return the file.
Collapse
Posted by Anjani Kumar on
Thanks Don . This has worked.