3

I am using well know React Framework

https://marmelab.com/admin-on-rest/RestClients.html

Now I want to upload a file to firebase, I follow the doc and find an awesome uploading component

FileInput (FileInput allows to upload some files using react-dropzone.)

so I used it in my react application and below is my code

export const PostCreate = (props) => (
    <Create {...props}>
        <SimpleForm>
            <ReferenceInput label="User" source="userId" reference="profiles" allowEmpty>
                <SelectInput optionText="name" />
            </ReferenceInput>
            <TextInput source="title" />
            <LongTextInput source="body" />


            <FileInput source="files" label="Related files" accept="video/*,audio/*, image/*, application/*" multiple={true} >
                <FileField source="src" title="title" />
            </FileInput>
        </SimpleForm>
     </Create>
);

For firebase interaction, I am using

https://github.com/sidferreira/aor-firebase-client

When I see my firebase console I can't see any file in firebase storage bucket but the filed value is saving right.

"blob:http://localhost:3000/8daedcb8-e526-427b-aa0c-83ff7ce9deee"

for a better idea, I have attached the snapshot of the firebase database and storage enter image description here enter image description here

I am new to this Framework so I know I surely have missed something but what I am still looking for.

So please help me to find the solution.

Nirmal
  • 939
  • 10
  • 24

2 Answers2

6

After spending 2 days with the problem, finally I able to find the solution and it is really quite easy. A simple method did the work for me.

First I have created a method for uploading the file to firebase

const upload = item => {
    return firebase.storage().ref().child("images/" + item.title).put(item.rawFile)
        .then((snapshot) => {
            // console.log('One success:', snapshot.downloadURL)
            return snapshot.downloadURL;
        }).catch((error) => {
            console.log('One failed:', item, error.message);
            return error;
        });
 }

and then I just updated my API decorator

addUploadFeature.js

with this method like this

const addUploadCapabilities = requestHandler => {
    return (type, resource, params) => {
        if (type === 'CREATE' && resource === 'posts') {
        // console.log("kgk");
            if (params.data.files && params.data.files.length) {
            // only freshly dropped pictures are instance of File
                console.log(params.data.files.length);
                const formerPictures = params.data.files.filter(
                    p => !(p.rawFile instanceof File)
                );
                const newPictures = params.data.files.filter(
                    function (p) {
                        // console.log(p);
                        const storageRef = firebase.storage().ref();

                        // const uploadTask = storageRef.child('images/' + p.title).put(p.rawFile);


                        return p.rawFile instanceof File;

                    }
                 );

                return Promise.all(newPictures.map(upload))
                    .then(function (base64Pictures) {
                          return base64Pictures.map(function (picture64) {
                                return ({
                                    src: picture64,
                                    title: `${params.data.title}`,
                                });
                            });
                        }
                    )
                     .then(function (transformedNewPictures) {
                             return requestHandler(type, resource, {
                                ...params,
                                data: {
                                    ...params.data,
                                    files: [
                                        ...transformedNewPictures,
                                        ...formerPictures,
                                    ],
                                },
                            });
                        }
                     );
            }
        }

        return requestHandler(type, resource, params);
    };
 };

That solves my issue. Now I can see my uploaded file to Firebase storage. enter image description here

And here is my database structure which I was looking for enter image description here

Nirmal
  • 939
  • 10
  • 24
0

First of all, note that admin-on-rest is not a CMS. It's a React framework you can use to build an administration application :)

Second, we don't assume anything about how you want to store your files. This is the job of your restClient. You can find an example showing how to handle file uploads in the documentation: https://marmelab.com/admin-on-rest/RestClients.html#decorating-your-rest-client-example-of-file-upload

Gildas Garcia
  • 6,966
  • 3
  • 15
  • 29
  • Thanks, I have corrected the mistakes. Now coming to the link you suggest i already visited that and i am using Firebase: sidferreira/aor-firebase-client for firebase interaction but the case in it saving the textfiled value but not uploading any files – Nirmal Feb 22 '18 at 10:19
  • The same recipe should be applicable to the firebase client. Decorate it with a function which will prepare the data for firebase storage – Gildas Garcia Feb 22 '18 at 11:41
  • Thanks, Gildas. Your suggestion really gave me the clue, now I am doing the work I will let you know once it will be done. – Nirmal Feb 22 '18 at 12:04
  • Great :) Would be even better to edit your answer with code samples specific to firebase if relevant. That will be really helpful for others ! – Gildas Garcia Feb 22 '18 at 13:03
  • sure I will do. I am just doing the work now once it will be done surely I will upload it. – Nirmal Feb 22 '18 at 13:06
  • 1
    I have posted my answer, Gildas. Please let me know if I can make it better. – Nirmal Feb 26 '18 at 13:09
  • I'm getting error on line: const formerPictures = params.data.files.filter( p => !(p.rawFile instanceof File) ); as **Undefined function filter** – rAVi Aug 30 '18 at 06:51
  • Please create a new question – Gildas Garcia Aug 30 '18 at 18:49