4

I have an app having firebase back-end. and when i made i didn't thought about the dimensions of images as if they ll be useful in future so i just kept the images as they are and kept their URLs in firestore.

But now i m in need of dimensions of images before showing them to user so i have thought of making a function that i ll execute only once in order to set the files with their dimension in firestore and i ll also add some client side code in order to get the dimensions before uploading them.

So i have tried almost everything to get the file dimensions in functions but couldn't do it.

sample[abstract code]

this code works in node.js but fails in firebase functions

    const fs = require('fs')
    const request = require('request')
    import sizeOf from 'image-size'
    const FIRE = 'https://firebasestorage.googleapis.com/file....'
    const FILE = 'file.jpg';

    request.head(FIRE, (err, res, body) => {
        request(FIRE)
            .pipe(fs.createWriteStream(FILE))
            .on('close', () => {
                sizeOf(FILE, (err1, dimensions) => {
                    const result = {
                        "width": dimensions.width,
                        "height": dimensions.height
                    }
                    console.log(dimensions.width, dimensions.height);
                    fs.unlinkSync(FILE);
                    response.setHeader('Content-Type', 'application/json');
                    const responseData = {
                        'Error': false,
                        'Message': "result : " + result
                    };
                    response.send(JSON.stringify(responseData));
                })
            })
    })

help me if someone knows something about this!

and moreover also tell me about how firebase keeps images, i mean in what manner ? whenever i open the url it doesnt show me the image instead it just downloads the image unline other urls on random websites.

I have got a trick to do it. It is quite prone to error, but will work for sure :

get all the urls of images using an api and do the stuff locally using node.js and post the result to another api, which will then feed the data to firestore ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Harkal
  • 1,770
  • 12
  • 28
  • "fails in firebase functions" What fails about this code when you run it in Cloud Functions? Is there an error message? Did you try adding additional logging to see what precise line fails? What image are you reading? How big is it? – Frank van Puffelen May 30 '19 at 01:54
  • @FrankvanPuffelen this is the error => Error: EROFS: read-only file system, open 'file.jpg' and i think this occurs because fs[file system] only works in server i think – Harkal May 30 '19 at 02:00
  • even before this, there had been times i faced such problem but different use case. there is no way firebase functions can handle data somewhere and keep it for some time for example in this case. functions are unable to keep the file in system so that i can run some operation on it – Harkal May 30 '19 at 02:08
  • If you want to write files, you need to write them to `/tmp` (also known as `tempfs`). See https://stackoverflow.com/questions/42719793/write-temporary-files-from-google-cloud-function – Frank van Puffelen May 30 '19 at 02:20

1 Answers1

3

Your code is trying to write to:

const FILE = 'file.jpg';

Which is a file in the same directory as where your index.js is stored. This is (as the error message says) a read-only directory in the Cloud Functions container. If you want to write any files, they must be in /tmp (also known as tempfs). See Write temporary files from Google Cloud Function

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807