I came across a strange issue while reading files from Ti.Filesystem.applicationDataDirectory. Whenever I want to access the text property of the Ti.Blob returned by Ti.Filesystem.File.read() I get null
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'foo.key');
var contents = f.read();
Ti.API.debug('contents: ' + JSON.stringify(contents));
var text = contents.text;
Ti.API.debug('text: ' + JSON.stringify(text)); // is NULL
The file was created like so
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'foo.key');
f.write(JSON.stringify({foo: 'bar'});
I'm developing on Android by the way with 6.0.1.GA
TL;DR it's the file extension that's causing the problem
When looking at the Ti.Blob more thoroughly I found that it says mimeType : application/gpg-keys. After updating my code above so it uses foo.json as filename everything works as expected.
Related
I am writing a script for Indesign that collects the contents from TextStyleRange objects, adds them to a javascript object, and should then write out a JSON file with the stringified JSON.
I can create the JSON just fine, with the contents from all of the TextStyleRanges in the current document, however the final file never gets written.
Here is a simplified example of what I have:
#include "json2.js";
main();
function main(){
myObj = { "key_1": "value_1", "key_2": "value_2" };
myContents = JSON.stringify(myObj, null, 4);
myFile = new File("~/Documents/myproject/en/translation.json");
myFile.open("w");
myFile.write( myContents );
$.writeln( myContents );
myFile.close();
}
In the VSCode debugger I can correctly see the JSON being output by $.writeln, however the file is not being created on disk. Any ideas what might be going wrong?
I'm using Windows 10, and Adobe Indesign 2022.
I have also tried with file edit but no luck:
myFile.open("e", "????", "????");
Actually it was simply that I needed to create the subfolder first:
var parentFolder = myFile.parent;
if (!parentFolder.exists && !parentFolder.create())
throw new Error("Cannot create file in path " + myFile.fsName);
After adding this to my script the folder and the file were created successfully.
I tried creating a method in Postman and got really close but am having issues with the signature. We are trying to query the IP ranges for VPCs to add to a WAF rule, in order to allow traffic to a secure application.
Postman allows a pre-request script, in Javascript, and supports a handful of included JS libraries, including CryptoJS.
The code here creates exactly the request that Ali Cloud says needs to be signed. It signs with HMAC-SHA1 from CryptoJS and encodes to base 64.
All of the variables are included in the request parameters. I'm not sure what else it could be complaining about.
var dateIso = new Date().toISOString();
var randomString = function(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
var accesskeyid = "LTAI4GC7VEijsm5bV3zwcZxZ"
var action = "DescribePublicIPAddress"
var format = "XML"
var regionid = "cn-shanghai-eu13-a01"
var signaturemethod = "HMAC-SHA1"
var signaturenonce = randomString(16)
var signatureversion = "1.0"
var timestamp = dateIso.replace(/:/gi, "%253A")
var version = "2016-04-28"
pm.environment.set("AccessKeyId", accesskeyid)
pm.environment.set("Action", action)
pm.environment.set("Format", format)
pm.environment.set("RegionID", regionid)
pm.environment.set("SignatureMethod", signaturemethod)
pm.environment.set("SignatureNonce", signaturenonce)
pm.environment.set("SignatureVersion", signatureversion)
pm.environment.set("Timestamp", dateIso)
pm.environment.set("Version", version)
var request = "GET&%2F&" + "AccessKeyID%3D" + accesskeyid + "%26Action%3D" + action + "%26Format%3D" + format + "%26RegionID%3D" + regionid + "%26SignatureMethod%3D" + signaturemethod + "%26SignatureNonce%3D" + signaturenonce + "%26SignatureVersion%3D" + signatureversion + "%26Timestamp%3D" + timestamp + "%26Version%3D" + version
pm.environment.set("Request", request)
var hash = CryptoJS.HmacSHA1(request, "spwH5dNeEm4t4dlpqvYWVGgf7aEAxB&")
var base64 = CryptoJS.enc.Base64.stringify(hash)
var encodesig = encodeURIComponent(base64)
pm.environment.set("Signature", encodesig);
console.log(base64)
console.log(request)
The console output shows:
Signature: XbVi12iApzZ0rRgJLBv0ytJJ0LY=
Parameter string to be signed:
GET&%2F&AccessKeyID%3DLTAI4GC7VEijsm5bV3zwcZvC%26Action%3DDescribePublicIPAddress%26Format%3DXML%26RegionID%3Dcn-shanghai-eu13-a01%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3DiP1QJtbasQNSOxVY%26SignatureVersion%3D1.0%26Timestamp%3D2020-06-01T15%253A38%253A12.266Z%26Version%3D2016-04-28
Request sent:
GET https://vpc.aliyuncs.com/?AccessKeyID=LTAI4GC7VEijsm5bV3zwcZvC&Action=DescribePublicIPAddress&Format=XML&RegionID=cn-shanghai-eu13-a01&SignatureMethod=HMAC-SHA1&SignatureNonce=iP1QJtbasQNSOxVY&SignatureVersion=1.0&Timestamp=2020-06-01T15:38:12.266Z&Version=2016-04-28&Signature=XbVi12iApzZ0rRgJLBv0ytJJ0LY%3D
Response Received:
<?xml version='1.0' encoding='UTF-8'?><Error><RequestId>B16D216F-56ED-4D16-9CEC-633C303F2B61</RequestId><HostId>vpc.aliyuncs.com</HostId><Code>IncompleteSignature</Code><Message>The request signature does not conform to Aliyun standards. server string to sign is:GET&%2F&AccessKeyID%3DLTAI4GC7VEijsm5bV3zwcZvC%26Action%3DDescribePublicIPAddress%26Format%3DXML%26RegionID%3Dcn-shanghai-eu13-a01%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3DiP1QJtbasQNSOxVY%26SignatureVersion%3D1.0%26Timestamp%3D2020-06-01T15%253A38%253A12.266Z%26Version%3D2016-04-28</Message><Recommend><![CDATA[https://error-center.aliyun.com/status/search?Keyword=IncompleteSignature&source=PopGw]]></Recommend></Error>
When I check the "server string to sign" from the response and the parameter string that was signed in a compare, they are identical.
It looks like everything is built as needed but the signature is still barking. Guessing I missed something simple but haven't found it yet.
Note: The accesskeyID and key posted are for example purposes and not a real account so this code will not copy and paste to execute in Postman.
PS - I learned quite a bit from the other few threads on this topic, which is how I got to this point. akostadinov was super helpful on another thread.
I believe you have double encoded &. I have implemented other Alibaba Cloud REST APIs successfully. Could you please check this.
Following is the expected string to sign format:
GET&%2F&AccessKeyId%3Dtestid&Action%3DDescribeVpcs&Format%3DXML&
SignatureMethod%3DHMAC-SHA1&SignatureNonce%3D3ee8c1b8-83d3-44af-a94f-4e0ad82fd6cf&SignatureVersion%3D1.0&TimeStamp%3D2016-02-23T12%253A46%
253A24Z&Version%3D2014-05-15
A bit late to the party, but as this is the first result when googling for the IncompleteSignature error, I thought I might comment and hopefully save someone else the grief I have been through.
For me, the subtle detail that I missed in the official documentation here is that the key used for the signature requires an ampersand & to be added to the end, before being used.
As soon as I caught that, everything else worked perfectly.
I copy a video into /Documents/videos/xxxxxx.mp4 and then, when I close the app and restart it, this error appear when I want to get the file:
nativeReason = "Could not open file stream for file at path: /var/mobile/Containers/Data/Application/055CE45C-28EC-46F5-9609-F16E357B682E/Documents/videos/1458044667778.mp4\nFile does not exist at path /var/mobile/Containers/Data/Application/055CE45C-28EC-46F5-9609-F16E357B682E/Documents/videos/1458044667778.mp4";
But I can see this file at the directory:
Whats the problem?? This occur on iOS, Ti.SDK 5.2.0.GA
My code:
if(OS_IOS){
Ti.API.debug("Media: " + this.get("media").nativePath);
var infile = Ti.Filesystem.getFile(this.get("media").nativePath); //!
}else{
var infile = Ti.Filesystem.getFile(this.get("media"));
}
Ti.API.debug('infile: ' + infile.exists());
var tempFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "videos/");
if(!tempFile.exists())
tempFile.createDirectory(); //create videos directory
var tempFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "videos/" + new Date().getTime() + ".mp4");
Ti.API.debug('tempFile ' + tempFile.exists()); // tempsFile is always empty
if(OS_IOS){
if (infile.exists() && (!tempFile.exists()) ) { //copy infile to videos/
tempFile.write(infile.read());
}
}else{ //Android
infile.copy(tempFile.nativePath); //copy infile to videos/
}
//Get video, the path saved is correct
var vid = Ti.Filesystem.getFile(video.get("videoFile"));
By default Ti.Filesystem.getFile() all relative paths are currently interpreted as relative to the Resources directory, not to the current context. This is a known issue that will be addressed in a future release. : http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Filesystem-method-getFile
So, you need to save the video file name in video.get("videoFile") and get the video this way : Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "videos/"+video.get("videoFile"));
I would like to check if the file exists and if not display noimage.gif. The below is returning false in the console, can you help?
...
[Bindable]
private var imageName:String;
imageName = "pca" + this._product.productID + ".jpg";
var fileName:File = new File()
fileName.nativePath = "C:/STSMultimediaSync/market/pictures/{imageName}";
trace(fileName.exists);
....
PS - I know the file exists as when I hard code the image name, the value changes to true.
I don't think you can do {} inside AS3 (It can be done in mxml). Try :
fileName.nativePath = "C:/STSMultimediaSync/market/pictures/"+imageName;
I trying to use ImageInfo and Jython to get information from a image on my harddrive.
I have imported the module fine but keep getting this error:
TypeError: setInput(): expected 2 args; got 1
And this is the code I am trying to use:
filename = "C:\\image.jpg"
img = ImageInfo.setInput(filename)
Could anyone point out what I am doing wrong.
Cheers
Eef
The missing argument Jython complains about is the ImageInfo object itself, which doesn't exist yet. You must construct it first. So:
filename = "C:\\image.jpg"
ii = ImageInfo()
img = ii.setInput(filename)
or
filename = "C:\\image.jpg"
img = ImageInfo().setInput(filename)
may work also.