how to check if a file path in cloud storage is valid or not using cloud function? - node.js

so I am trying to download an image from firebase storage like this using cloud function
const bucket = storage.bucket("myApp.appspot.com")
const filePath = `eventPoster/${creatorID}/${eventID}.png`
await bucket.file(filePath).download({
destination: tmpFilePath
})
but the problem is, I can't ensure the image is always in the png format, it can also be jpeg or jpg
I will get error like this if I hard code it like that
Error: No such object: myApp.appspot.com/eventPoster/user1/ev123.png
so how to check if a path is valid or not or how to make a code that can work dynamically with other image extension ?

Posting as Community Wiki, because I based part of it in a comment and so other members of the Community can feel free to edit it.
As mentioned in the comments, you can use the method exists(), to check if a specific file exists in your bucket. This way, you will not an issue, in case the file doesn't exist when you are trying to return it. This would be the best way for you to check only for files existing, with the name based in the ids as you are basing your name structure.
Besides that, as clarified in this other post from the Community here, you can restrict the file types in your bucket in case you are using the POST method to upload files, otherwise, you won't be able - which can also, be an alternative. In case you are not using POST, as mentioned as well in this other post, you can try to construct a Cloud Function that will check for the file type before it uploads the file to the bucket, which would make your application only upload specific files.
In case you would like to check on how to upload files to Cloud Storage using Cloud Functions, you can check this tutorial here: How to upload using firebase cloud functions
Unfortunately, these seem to be the only available options right now, but I think they are a good start for you.
Let me know if the information helped you!

You can use the exists method on a file to check if your PNG is present or not. You can also change your code and use the getFiles() method. As you can see, you can put options, and one named "prefix". In your case you can have this
bucket.getFiles({
prefix: "eventPoster/${creatorID}/${eventID}"
}, <callback>);
Here you will list all the file with the prefix: PNG, JPEG and others. make your code smarter to leverage this dynamic answer

Related

Entering a proper path to files on DBFS

I uploaded files to DBFS:
/FileStore/shared_uploads/name_surname#xxx.xxx/file_name.csv
I tried to access them by pandas and I always receive information that such files don't exist.
I tried to use the following paths:
/dbfs/FileStore/shared_uploads/name_surname#xxx.xxx/file_name.csv
dbfs/FileStore/shared_uploads/name_surname#xxx.xxx/file_name.csv
dbfs:/FileStore/shared_uploads/name_surname#xxx.xxx/file_name.csv
./FileStore/shared_uploads/name_surname#xxx.xxx/file_name.csv
What is funny, when I check them by dbutils.fs.ls I see all the files.
I found this solution, and I tried it already: Databricks dbfs file read issue
Moved them to a new folder:
dbfs:/new_folder/
I tried to access them from this folder, but still, it didn't work for me. The only difference is that I copied files to a different place.
I checked as well the documentation: https://docs.databricks.com/data/databricks-file-system.html
I use Databricks Community Edition.
I don't understand what I'm doing wrong and why it's happening like that.
I don't have any other ideas.
The /dbfs/ mount point isn't available on the Community Edition (that's a known limitation), so you need to do what is recommended in the linked answer:
dbutils.fs.cp(
'dbfs:/FileStore/shared_uploads/name_surname#xxx.xxx/file_name.csv',
'file:/tmp/file_name.csv')
and then use /tmp/file_name.csv as input parameter to Pandas' functions. If you'll need to write something to DBFS, then you do other way around - write to local file /tmp/..., and copy that file to DBFS.

Autodesk Forge: Industrial Construction Demo bucket not working

I have cloned the Industrial Construction Demo code https://github.com/petrbroz/forge-industrial-construction but I am having issues with the 'bucket-name'.
In the launch file you need to specify "FORGE_BUCKET": "" instead of the usual URN.
I have written the name of the bucket that contains my file but it does not work. I have tried debugging it but still no luck.
There is no exemplary bucket or files, so I would really like to know:
-What do I need to put in as the bucket name?
-What files need to be in the bucket?
-What file types?
-Any extra information to help me figure out why it isnt working.
Many thanks,
Poppy
My apologies, I haven't explained this in the code sample very well. The demo requires files in Forge to be named in a specific way. I have just updated the README with more details: https://github.com/petrbroz/forge-industrial-construction.

Concatenate Videos with node.js in AWS Lambda

Im wondering if it's possible to merge (concatenate) two video clips into one with an AWS Lambda function using Node.js?
The dependency that I was going to use is ffmpeg (or fluent-ffmpeg), but from what I can tell, all the APIs use the actual file path, not the file variables. I download the actual video files from an S3 bucket so I can't use the path's... can I?
I haven't found a single example that uses file variables. If there's a better way to do this that Im not thinking of, feel free to share! Thanks for the help as always.
I found a way around it! All I needed to do was save the object body from the item I get from S3 to a file on the local Lambda file system. Not sure why I didn't think of this in the first place. I ended up doing
fs.writeFile("out.mp4", item.Body, (err) => {
if(err) console.log(err);
console.log("Body saved!")
});
Hopefully this answer helps someone that comes across a similar mental block in the future.

Secondary Tile with Icon stored in Application Temporary Storage

I am trying to create a SecondaryTile using a JPG that I extracted to temporary storage (). When I create a URI from this file I get an error from the constructor of the SecondaryTile class. The error message I get is very helpful, it says "Incorrect parameter".
I have tried passing in a URI to the filename like this:
new Uri("file://C:/Users/{username}/AppData/Local/Packages/{myAppPAckage}/TempState/{filename}.jpg");
I have also tried ms-appx even though I don't think that is the right way given my file is stored in temporary app storage.
new Uri("ms-appx:///C:/Users/{username}/AppData/Local/Packages/{myAppPAckage}/TempState/{filename}.jpg");
Using ms-appx:/// as the prefix allows the tile to be pinned without error but the image does not display.
The file system path that I am getting is obtained from ApplicationData.Current.TemporaryFolder.
I found this documentation that provided the uri prefix for the folder I am using to source the image.
new Uri("ms-appdata:///temp/{filename}.jpg");
Unfortunately, this also gives the Incorrect parameter error.
How do I use an image file that is stored in App Temporary Storage?
I needed to use ApplicationData.Current.LocalFolder and the prefix "ms-appdata:///Local".

Getting the full path of a DirectoryEntry

Does anyone know how to get the full path of a DirectoryEntry object in a Chrome Packaged App, without any tildes or other shortcuts?
I am writing a Google Chrome Packaged App. My app has a button where a user can choose a directory using chrome.fileSystem API. When the directory choice comes back to my app, it is represented by a DirectoryEntry object, which is defined in the File API. The object looks like this in the console:
DirectoryEntry {
filesystem: DOMFileSystem
fullPath: "/to_read"
isDirectory: true
isFile: false
name: "to_read"
__proto__: DirectoryEntry
}
I am using Windows and the full path of the directory is
C:\Users\David\Desktop\to_read
I would like a function that can return that path or something close. Unfortunately, the closest thing I found is chrome.fileSystem.getDisplayPath, but that returns the following:
~\Desktop\to_read
The return value from getDisplayPath is not useful to me, because I want to get the full name of the directory (including the drive) so I can compare it to some other full directory paths I have.
I tried calling toURL() on the DirectoryEntry and it returned an empty string.
A bit about my project: I want to write an iTunes library synchronizer as a Chrome Packaged App. The iTunes library XML file contains full paths like file://localhost/D:/David/Music/Bob/Bob%20Album/01%20Bob.mp3. The user will give my app access to his music folders, and I want to be able to tell if he gave me access to the right folders.
The only full paths available are those returned by getDisplayPath.
The mediaGalleries API may be a better fit for your project: http://developer.chrome.com/apps/mediaGalleries.html#iTunes.
If you have a full path path/to/the/file.mp3 and you want to load the file directly with tis you can do it but the solution is not perfect. Use the chrome.mediaGalleries API to ask where the user saves his music (in your case), then you can write a loop who check if one of the path repository is equal to a gallery.
For example if you got a file path/to/the/file.mp3 from the xml file and your galleries list looks like ["D", "to", "Videos"], write a function to check if each component of the file's path is a gallery. In this case your code will find "to", so you can launch a second function who use "the/file.mp3".
The second function has to use the given path and find if the gallery contains the right folders and finally the right file (use this example by Google). In the case you're trying to find "the/file.mp3" with the gallery to your loop has to find a directory named "the" then "file.mp3" (write a recursive function), if you find the file open it, otherwise come back to the first function if you haven't check all the galleries or all the path's component.
This is currently (2014-01-10) not a feature of Chrome, but I have suggested it and they are working on it:
https://code.google.com/p/chromium/issues/detail?id=322952

Resources