Flutter Firebase Storage Add additional parameters to end of download url - flutter

I have an issue regarding the integration of downloaded photos from Firebase storage in my Flutter app.
I am using the https://pub.dev/packages/gallery_saver package to download the images to the device from the user. Due to a bug/the concept of the plugin you are only able to download images if the url ends with e.g. jpeg/png/jpg etc.
Here some other comments of people who have the same issue withe the package: https://github.com/CarnegieTechnologies/gallery_saver/issues/66
To use the package with my app now I am adding the filename to the end of the image url.
This works completely fine when my security rules allow all reads.
As soon as I add these rules:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read: if request.auth != null;
}
}
}
I get an error 403 forbidden on downloading all images.
Is there a way to make this work while using file endings at the end of the url?

Download URLs generated by Firebase for Cloud Storage are opaque URLs, and you can't modify them.
Your options are to:
Mark the file as public on Cloud Storage itself, so that you can download it without the extra token in the URL.
Fix the plugin you use to allow downloading without a filename extension, in which case it can determine the file type from its metadata.
Expand the plugin you use to allow downloading the file through the Firebase SDK, instead of through a download URL.

Related

Read the contents of a .csv file in Google Drive

I have a public .csv file on my Google Drive and I want to pull the file and display it in my flutter app. I figured that I need the fileID, the OAuth token and the client ID,I already got these. I also have a fully working sign-in page. But I can't figure out what is the exact methodology of fetching the file. I know that there is some content of this exact question online, I am a beginner, those posts were old, and some functions were deprecated, I couldn't understand them.
You can use the export api to get this
"https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]"
Refer https://developers.google.com/drive/api/v3/reference/files/export for more details
Please note that is restricted to 10 Mb per file
Edit
add these dependencies
dependencies:
googleapis: any
google_sign_in: any
Implement google signing
final googleSignIn = signIn.GoogleSignIn.standard(scopes: [drive.DriveApi.DriveScope]);
final signIn.GoogleSignInAccount account = await googleSignIn.signIn();
Create a firebase project and enable google signin and link it with your project. Download its Json and add it to the project too.
Goto cloud console and enable Google Drive API
After the login is complete you can use the download method to download the file
Future<File> download(String id, String filename, {Function(int, int) onDownloadProgress});
Check this tutorial for reference
https://betterprogramming.pub/the-minimum-guide-for-using-google-drive-api-with-flutter-9207e4cb05ba

Firebase Storage: Public to Internet about files under a specific directory

I want to publish all the files under a specific directory to the Internet.
Security Rules
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /public/{allPaths=**} {
allow read;
allow write: if request.auth != null;
}
}
}
After writing the security rules as described above, when I open the Browser of Cloud Storage:
The files under the directory are still set to private.
If you look at the details of each file, you will see that no public URL has been issued.
And when I enter the Authenticated URL, you will be redirected to Google's login page.
How can I fix this?
Firebase security rules only control who can get the URL of a specific object either by Firebase SDK or REST APIs. If you check for object URL in Firebase console, you'll see an URL of format which is probably what you are looking for:
https://firebasestorage.googleapis.com/v0/b/[PROJECT].appspot.com/o/public%2Fdog.jpg?alt=media&token=fd991fdf-a33f-4321-9017-09907e1a5243
Security rules can only restrict users from getting this URL at first place however anyone with this URL can access the image/file (if an authorized user has shared the URL).
That being said, the rule allow read: if true; will allow anyone to get the public (like the one shown above) and they can access it.
If you want a public link from GCS as well then you would have to edit permissions from GCP console and allow allUsers to read it:

How to remove query string from Firebase Storage download URL

Problem:
I need to be able to remove all link decoration from the download URL that is generated for images in Firebase Storage.
However, when all link decoration is stripped away, the resulting link currently would return a JSON document of the image's metadata.
Context:
The flow goes as follows:
An image is uploaded to Firebase from an iOS app. Once that is done the download URL is then sent in a POST request to an external server.
The server that the URL is being sent to doesn't accept link decoration when submitting image URLs.
Goal:
Alter the Firebase Storage download URL such as it is stripped of all link decoration like so:
https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg
Notes:
The problem is twofold really, first the link needs to be manipulated to remove all the link decoration. Then the behavior of the link needs to changed, since in order to return an image, you need ?alt=media following the file extension, in this case .jpg. Currently, without link decoration, using the link with my desired structure would return a JSON document of the metadata.
The current link structure is as follows:
https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg?alt=media&token=[TOKEN]
Desired link structure:
https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg
The token is necessary for accessing the image depending security rules in place, but can be ignored with the proper read permissions. I can adjust the rules as needed, but I still need to be able to remove the ?alt=media and still return an image.
Building up on Frank's answer, if you access to your associated Google Cloud Platform project, find the bucket in the Storage tab and make this bucket public, you will be able to get the image from here with the format you wish. That is, you will not be accessing through Firebase
https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg
but through Google Cloud Storage, with a link like
https://storage.googleapis.com/[bucket_name]/[path_to_image]
Once in your GCP project Console, access the Storage bucket with the same name as the one you have in your Firebase project. They are the same bucket. Then make the bucket public by following these steps. After that, you will be able to construct your links as mentioned above and they will be accessible with no token and no alt=media param. If you do not want to make the public to everyone, you will be able to play around with the permissions there as you wish.
You could split the url string into two halves by using String.componentsSeparatedByString(_ separator:)
Storage.storage().reference().child(filePath).downloadURL(completion: { (url, error) in
let urlString = url.absoluteString
let urlStringWithoutQueryString = urlString.componentsSeparatedByString("?").first!
})
Calling .downloadURL on a StorageReference will return you that URL, but this method can be used to remove the query string from any URL. String.componentsSeparatedByString(_ separator:) breaks a String into an array of Strings, splitting the string by any occurrence of a given separator, in this case ?.
NOTE this method assumes that ? occurs only once within the url string, which I believe is the case for all Firebase Storage urls.
You should treat the download URL that you get back from Firebase as an opaque string. There's no way to strip the parameters from a download URL without breaking that download URL.
If you want to allow public access to the files in your bucket with simpler URLs, consider making the object in your (or even your entire) bucket public.

File Upload: File Upload URL not provided

I've been trying to get file uploads to work, following the instructions for both Dropbox and S3 but each time I just get this message:
File Upload URL not provided
It doesn't seem to be making any calls to the server. I've found this mention of a bug around file uploads:
https://github.com/formio/ngFormio/issues/322
But I suspect that applies if you're hosting it yourself. I'm using the cloud version.
I've configured it with e.g. the S3 bucket's URL, authentication etc.
What does this error actually mean?
Update: here's the syntax I'm using:
<formio form="https://formview.io/#/xxxxxxxxxxxxxxxxxxx/applicationform" url="'https://formview.io/#/xxxxxxxxxxxxxxxxxxx/applicationform'"></formio>
Thanks
In order to make the uploads work, you need to provide the URL of your form, which is used to generate the upload token to upload the files to the 3rd party providers. This can be done in one of two ways.
<formio src="'https://examples.form.io/example'"></formio>
You would use above if you wish to render the form from the JSON REST API of the form. In many cases, you may wish to provide the actual form object (which I suspect is what you are doing) like so.
<formio form="{...}"></formio>
This works fine for rendering the form, but it does not provide the URL context for file uploads. For this reason, we have the url parameter which you can include along with your form object for file uploads to work.
<formio form="{...}" url="'https://examples.form.io/example'"></formio>
Providing the url this way is passive. The form will not try to submit to that url, but rather just use it as the url configuration for file uploads.

How to show images in other domains / Chrome Packaged Apps

I have a JSON which returns a list of URLs of images to access the JSON that is already placed in a field in this domain whitelist manifest.json, however when I try to view the pictures it complains that it can not access the images.
1 - How to Perm can display images that are not within the package App
2 - How can I download the images to download the APP and then display
to this second question I used the RAL, a lib trial google and it worked, however I could not make a test using this publication lib he claims an error, follow the link to the image lib and complaining about the error:
lib: https://github.com/GoogleChrome/apps-resource-loader
Error: http://twitter.yfrog.com/oe24998653p
You can request external images using XMLHttpRequest and transform them into ObjectURLs. Then set the src attribute in the <img> tag to each ObjectURL and it should work.
Since this is a very common use case, we created a library to simplify it. Just drop the apps-resource-loader ral.min.js to your project and then:
var remoteImage,
container = document.querySelector('.imageContainer'),
toLoad = { 'images': [
'http://myserver.com/image1.png',
'http://myserver.com/image2.png' ] }; // list of image URLs
toLoad.images.forEach(function(imageToLoad) {
remoteImage = new RAL.RemoteImage(imageToLoad);
container.appendChild(remoteImage.element);
RAL.Queue.add(remoteImage);
});
RAL.Queue.setMaxConnections(4);
RAL.Queue.start();
Remember that you need permission in the manifest.json to all domains you will be XHR'ing to. If you don't know beforehand where those images will be hosted, you can ask permission for any url:
permissions: ['<all_urls>'],
For other usages and to get the full library, please see the project page:
https://github.com/GoogleChrome/apps-resource-loader
and a simple demo at:
https://github.com/GoogleChrome/apps-resource-loader/tree/master/demo
Google has added a browser tag that allows you to include images, but so far I haven't been able to get it to work.
Here is what they show as an example
<browser src="http://i.stack.imgur.com/dmHl0.png">