Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

File saveAsOpen and Base64 encoding...

New Here ,
Aug 28, 2008 Aug 28, 2008
In order to send the activeDocument to a web service, i need to save is as a file image, and send in an xml format.
Thus, i also need to encode it in Base64.
At this point everything is OK.

But between the saveAs of the file, its reopening, and its encoding, its corrupted.
I receive an error on the Mongrel server.

The Base64 encoder seems to work well, the rest of the code seem to be also ok, so I think my problem is either I do not save it correctly, either i do not reopen it correctly...

Please save me 😞

Here is the code :

/*********************************************/
/* STEP 1 : save current document as image file (temporary) */
/******************************************/
var docRef = activeDocument;
var filepath=app.path.toString()+"/"+docRef.name+".jpg";//create the image file in the installation folder of Photoshop
var file = new File(filepath);
//var options = new ExportOptionsSaveForWeb();
//options.format = SaveDocumentType.PNG;
var options = new JPEGSaveOptions();
options.quality=8;
docRef.saveAs (file, options, true, Extension.LOWERCASE);
//docRef.exportDocument (file, ExportType.SAVEFORWEB , options);
file.close();

/********************************************************/

/* I code here dialogBox and httpCOnenction object creation
that do not need to be written here
(but if you think it's important, i can give you the full script)
*/

var f= new File(filepath);
f.open();
var buffer = f.read(f.length);
f.close();

/*
I build an HttpConnection object called "send"
*/ send.request='titleforyourimage"+f.name+""+f.length+""+base64encode(buffer)+"';

Here is the Server error :

Exception working with image: Not a JPEG file: starts with 0xc7 0xff `/var/folders/Nz/NzlixjchF+WAvFkZK9vVRU+++TM/-Tmp-/test599-0.jpg'
TOPICS
Actions and scripting
6.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Explorer ,
Aug 28, 2008 Aug 28, 2008
> /*
> I build an HttpConnection object called "send"
> * / send.request='"+f.name+""+f.length+""+base64encode(buffer)+"';
>
> Here is the Server error :
>
> Exception working with image: Not a JPEG file: starts with 0xc7 0xff `/var/folders/Nz/NzlixjchF+WAvFkZK9vVRU+++TM/-Tmp-/test599-0.jpg'


Your Ps/JS code looks fine. I hate to sound like a moron, but have you tried
immediately re-opening the file in Ps (before the base64 conversion) to make
sure Ps is saving it correctly? If you can, Ps is not the problem and it's more
likely an http/html problem.

You may also want to track down the jpeg spec and find out what is permissible
for the first couple of bytes in a jpeg file.

I would also advise against creating docs in the Ps app directory 🙂

-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 28, 2008 Aug 28, 2008
no problem to open it in PS, and when i look in the directory, the file exist, and have the same size a if saved manually...
But in the script when try some alerts, the size indicated o not correspond.. very wired.

What di you mean by :
"You may also want to track down the jpeg spec and find out what is permissible
for the first couple of bytes in a jpeg file."
?

Encoding a Jpeg in Base64 works fine in Java if it's what you mean..

I took the encoder from internet, thus i suppose it is ok, but i really don't know because the algorithm uses bit shifts...

Here is all its code :

var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

function base64encode(st) {
var str = new String(st);
var out, i, len;
var c1, c2, c3;

len = str.length;
i = 0;
out = "";
while(i < len) {
c1 = str.charCodeAt(i++) & 0xff;
if(i == len)
{
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt((c1 & 0x3) << 4);
out += "==";
break;
}
c2 = str.charCodeAt(i++);
if(i == len)
{
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += base64EncodeChars.charAt((c2 & 0xF) << 2);
out += "=";
break;
}
c3 = str.charCodeAt(i++);
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
out += base64EncodeChars.charAt(c3 & 0x3F);
}
return out;
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 28, 2008 Aug 28, 2008
Let's give you everything in fact, it will be more simple (by the way the code is suppose to become OpenSource)<br /><br />I work on Mac, Photoshop CS3 (10.0.1)<br />I use JavaScript<br />HTTP request received by a Mongrel server (Ruby on Rails)<br /><br />// Copyright 2008. Studio Melipone. All rights reserved. <br />// Licence GNU LGPL<br />// Send the active document to the UpShot web service (http://upshotit.com)<br />// The document is sent as a .png file, as a draft on the user's account.<br />// Therefore you must have a document opened and Adobe Bridge installed to perform this script.<br /><br />/*<br /> <javascriptresource><br /> <name>UpShot</name><br /> <type>automate</type><br /> <about><br /> Script for Upshot <br /> Copyright 2008 Studio Melipone <br /> http://upshotit.com <br /> </about><br /> <enableinfo>true</enableinfo><br /> </javascriptresource> <br />*/<br /><br />#target photoshop<br />#include "Base64.jsx"<br /><br />app.bringToFront();<br /><br />if( documents.length==0)// is a document opened ?<br /> alert("There are no Photoshop documents opened !")<br />else {<br /> /*********************************************/<br /> /* STEP 1 : save current document as image file (temporary) */<br /> /******************************************/<br /> var docRef = activeDocument;<br /> var filepath=app.path.toString()+"/"+docRef.name+".jpg";//create the image file in the installation folder of Photoshop<br /> var file = new File(filepath);<br /> //var options = new ExportOptionsSaveForWeb();<br /> //options.format = SaveDocumentType.PNG;<br /> var options = new JPEGSaveOptions();<br /> options.quality=8;<br /> docRef.saveAs (file, options, true, Extension.LOWERCASE);<br /> //docRef.exportDocument (file, ExportType.SAVEFORWEB , options);<br /> file.close();<br /> <br /> /********************************************************/<br /> <br /> // Only Bridge can use HttpConnection, so we test if it is installed<br /> var bridgeTarget = BridgeTalk.getSpecifier(getAppSpecifier("bridge")); <br /> <br /> if( !bridgeTarget ) { <br /> alert("Adobe Bridge not installed, needed to continue."); <br /> } <br /> else { <br /> preferences.rulerUnits = Units.PIXELS;<br /> displayDialogs = DialogModes.NO;<br /> <br /> /**********************************/<br /> /* STEP 2 : retrieve user's login & password */<br /> /*******************************/<br /> <br /> res = "dialog { text: 'UpShot authentication', \<br /> info: Panel { orientation: 'column', alignChildren:'right', \<br /> text: 'Please Identify Yourself', \<br /> login: Group { orientation: 'row', \<br /> s: StaticText { text:'Login :' }, \<br /> e: EditText { characters: 30 } \<br /> }, \<br /> passwd: Group { orientation: 'row', \<br /> s: StaticText { text:'Password :' }, \<br /> e: EditText { characters: 30, properties:{noecho: true} }, \<br /> } \<br /> }, \<br /> buttons: Group { orientation: 'row', \<br /> okBtn: Button { text:'OK', properties:{name:'ok'} }, \<br /> cancelBtn: Button { text:'Cancel', properties:{name:'cancel'} } \<br /> } \<br /> }"; <br /> <br /> dlg = new Window (res); <br /> dlg.center(); <br /> dlg.show(); <br /><br /> var login = dlg.info.login.e.text;//retrieve the values given in the form<br /> var pass = dlg.info.passwd.e.text;<br /><br /> /******************************/<br /> /* STEP 3 : send image through Bridge */<br /> /***************************/<br /> var f= new File(filepath);<br /> f.open();<br /> var buffer = f.read(f.length);<br /> f.close();<br /> <br /> alert("file size "+file.length);<br /> alert("f size "+f.length);<br /> alert("BUF "+buffer);<br /> alert(base64encode("B64 "+base64encode(buffer)));<br /> <br /> // create a new BridgeTalk message object <br /> var bt = new BridgeTalk; <br /> // target the Adobe Bridge application <br /> bt.target = bridgeTarget; <br /> //p173 of Javascript Tools Guide for CS3 for http message<br /> bt.body = "\<br /> if(!ExternalObject.webaccesslib ) {\<br /> ExternalObject.webaccesslib = new ExternalObject('lib:webaccesslib');\<br /> }\<br /> var http = new HttpConnection('http://127.0.0.1:3000/en/users/get_id.xml') ; \<br /> var idfile = new File('"+app.path.toString()+"/id.xml') ;\<br /> http.response = idfile ; \<br /> http.username = '"+login+"';\<br /> http.password = '"+pass+"';\<br /> http.mime='text/xml';\<br /> http.responseencoding='utf8';\<br /> http.execute();\<br /> http.response.close();\<br /> http.close();\<br /> idfile.open();\<br /> var send = new HttpConnection('http://127.0.0.1:3000/en/users/'+idfile.read()+'/upshots') ; \<br /> send.method = 'POST';\<br /> send.username = '"+login+"';\<br /> send.password = '"+pass+"';\<br /> send.mime='text/xml';\<br /> send.requestheaders=['Host','http://localhost:3000'];\<br /> send.requestheaders=['Accept','*/*'];\<br /> send.requestheaders=['Content-Type','text/xml'];\<br /> send.request='<upshot><title>titleforyourimage</title><file_name>"+f.name+"</file_name><size>"+f.length+"</size><javafile>"+base64encode(buffer)+"</javafile></upshot>';\<br /> send.execute();\<br /> idfile.toSource();\<br /> ";<br /> <br /> bt.onResult = function(result) { <br /> object = bt.result = eval(result.body);<br /> //file.remove();<br /> //object.remove();<br /> //bridge.quit ();<br /> return eval(result.body); <br /> } ;<br /> <br /> bt.onError = function( message ) { <br /> var errCode = parseInt (message.headers ["Error-Code"]); <br /> throw new Error (errCode, message.body); <br /> } ;<br /> <br /> //send the message ( also launch the targetted application)<br /> bt.send(10);<br /> <br /> /**********************************************/<br /> /* STEP 4: Once all done, delete the image previously created */<br /> /*******************************************/<br /> }<br />}<br /><br />//////////////////////////////////////////////////////////////////<br />/////////////////////////////////////////////////////////////////<br />/*functions from http://www.ps-scripts.com/bb/viewtopic.php?t=1282 */<br />//////////////////////////////////////////////////////////////<br />/////////////////////////////////////////////////////////////<br /><br />function getAppSpecifier(appName) {<br /><br /> if (isCS2()) {<br /> if (appName == 'photoshop') {<br /> return 'photoshop-9.0';<br /> }<br /> if (appName == 'bridge') {<br /> return 'bridge-1.0';<br /> }<br /> // add other apps here<br /> }<br /><br /> if (isCS3()) {<br /> if (appName == 'photoshop') {<br /> return 'photoshop-10.0';<br /> }<br /> if (appName == 'bridge') {<br /> return 'bridge-2.0';<br /> }<br /> // add other apps here<br /> }<br /><br /> return undefined;<br />};<br /><br />function isCS2() {<br /> var appName = BridgeTalk.appName;<br /> var version = BridgeTalk.appVersion;<br /><br /> if (appName == 'photoshop') {<br /> return version.match(/^9\./) != null;<br /> }<br /> if (appName == 'bridge') {<br /> return version.match(/^1\./) != null;<br /> }<br /><br /> return false;<br />};<br />function isCS3() {<br /> var appName = BridgeTalk.appName;<br /> var version = BridgeTalk.appVersion;<br /><br /> if (appName == 'photoshop') {<br /> return version.match(/^10\./) != null;<br /> }<br /> if (appName == 'bridge') {<br /> return version.match(/^2\./) != null;<br /> }<br /><br /> return false;<br />};
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 28, 2008 Aug 28, 2008
GreGeeK@adobeforums.com wrote:
> Let's give you everything in fact, it will be more simple (by the way the code is suppose to become OpenSource)
>
> I work on Mac, Photoshop CS3 (10.0.1)
> I use JavaScript
> HTTP request received by a Mongrel server (Ruby on Rails)

OK. I'll try this out later. I've got the same except Mongrel, but that
shouldn't be a problem.

-X
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Aug 28, 2008 Aug 28, 2008
Mabye it's my turn to sound like a moron but I think you need to set the file encoding to BINARY when you read it back in for the base64 encoding.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 28, 2008 Aug 28, 2008
doesn't sound like moron for me (i'm quite noob in Ps scripting)
All your ideas are welcome people !!

something like that ?

/******************************/
/* STEP 3 : send image through Bridge */
/***************************/
var f= new File(filepath);
f.open();
f.encoding = 'BINARY';
var buffer = f.read(f.length);
f.close();

This way is not working, same error. Maybe i do not do it the good way...
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Aug 28, 2008 Aug 28, 2008
One last thought then. I would try the base64 encoding in Photoshop then send the encoded buffer via Bridgetalk. My guess is Bridgetalk doesn't like the binary string.

var f= new File(filepath);
f.open();
f.encoding = 'BINARY';
var buffer = f.read(f.length);
f.close();
buffer = base64encode(buffer);//encode here then send to Bridge
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 28, 2008 Aug 28, 2008
Michael_L_Hale@adobeforums.com wrote:
> f.encoding = 'BINARY';

The 'BINARY' thing shouldn't be needed. It's a text file and is byte-order
independent as well. It's like doing a hex conversion with fewer bytes.

If he can encode and decode the file in JS and doesn't have any problems, the
issue is elsewhere, which is what my guess is.

-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Aug 28, 2008 Aug 28, 2008
Xbytor@adobeforums.com wrote:
>The 'BINARY' thing shouldn't be needed. It's a text file and is byte-order independent as well. It's like doing a hex conversion with fewer bytes.

I guess I miss read the script. I thought it saved the current doc as a jpeg then read that jpg into a buffer that is sent to Bridge.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 28, 2008 Aug 28, 2008
> I guess I miss read the script. I thought it saved the current doc as a jpeg then read that jpg into a buffer that is sent to Bridge.

Nope. You were right. I misread the script this time. I tend to do that when I'm
this badly hungover :).

You do need the 'BINARY' encoding for the open().

Here's an UNTESTED reworking of your script. It has a some interesting features
that I've started using in all of my Ps->Br work.

1) For testing, you can now also run this in ESTK with bridge as the target.
This will make debugging the bridge code a lot easier. You have to hardwire
file, idfile, login, and pass for this to work correctly.

2) The bridge function is now an actual function instead of a string. Again,
this makes it far easier to debug.

You also may want to consider doing the base64 encoding on the bridge side
instead of on the Ps side instead of sending the entire file over BridgeTalk.

You also may want to consider doing a normal HTTP file upload provided
HttpConnection permits it.

The code is here:
http://www.ps-scripts.com/bb/viewtopic.php?t=2191

I couldn't post it here for some reason.

-X
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 29, 2008 Aug 29, 2008
LATEST
well thanks all of you !!!
now its seems that it is not the way to encode, because encoding/decoding is ok on the javascript side.
I tried several things and the Base64 code sent to our server is diffrent from the one it received !!!!
i'm not kidding, we send 64 bytes of data, the server only receive 16... WTF?!
working so fine with the Java plugin that does the same thing...
(http://github.com/jbfeldis/upshot-smash-uploader)
We're trying to understand that...
Thanks afterall for all your efforts.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines