★ wanayoo — archive 1999 https://github.com/api-platform/admin/issues/326Nouvelle recherche | Portail wanayoo
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload file issue #326

Open
BenPilliez opened this issue Oct 7, 2020 · 0 comments
Open

Upload file issue #326

BenPilliez opened this issue Oct 7, 2020 · 0 comments

Comments

@BenPilliez
Copy link

@BenPilliez BenPilliez commented Oct 7, 2020

Hi, guys i'm uploading file using this code

const entrypoint = process.env.REACT_APP_API_ENTRYPOINT;
const fetchHeaders = {Authorization: `Bearer ${window.localStorage.getItem("admin_token")}`};
let fetchHydra = (url, options = {}) => baseFetchHydra(url, {
    ...options,
    headers: new Headers(fetchHeaders),
});


const UserList = props => (
    <ListGuesser {...props}>
        <FieldGuesser source="username"/>
        <FieldGuesser source="email"/>
        <FieldGuesser source="isVerified"/>
    </ListGuesser>
)
const GameImageList = props => (
    <ListGuesser {...props}>
        <FieldGuesser source="filePath"/>
        <FieldGuesser source="game"/>
    </ListGuesser>
);

const GameShow = (props) => (
    <ShowGuesser {...props} >
        <ImageField source="contentUrl" title="title"/>
    </ShowGuesser>
)

const GameImageCreate = props => (
    <CreateGuesser {...props}>
        <ImageInput source="file" label="Game Pictures" accept="image/*">
            <ImageField source="src" title="title"/>
        </ImageInput>
    </CreateGuesser>
);

const GameImageUpdate = props => (
    <CreateGuesser {...props}>
        <ImageInput source="filePath" accept="image/*">
            <ImageField source="src" title="title"/>
        </ImageInput>
        <InputGuesser source={"game"}/>
    </CreateGuesser>
);

const GameCreate = props => (
    <CreateGuesser {...props}>
        <InputGuesser source={"name"}/>
        <RichTextInput source={"text"}/>
        <InputGuesser source={"rate"}/>
        <ReferenceInput
            source="gameImage"
            reference="game_images"

        >
            <AutocompleteInput optionText="@id"/>

        </ReferenceInput>
    </CreateGuesser>
)

const GameEdit = props => (
    <EditGuesser {...props}>
        <InputGuesser source={"name"}/>
        <RichTextInput source={"text"}/>
        <InputGuesser source={"rate"}/>
        <ReferenceInput
            source="gameImage"
            reference="game_images"

        >
            <AutocompleteInput optionText="@id"/>

        </ReferenceInput>
    </EditGuesser>
);
const apiDocumentationParser = entrypoint => parseHydraDocumentation(entrypoint, {headers: new Headers(fetchHeaders)})
    .then(
        (({api}) => {
            api.resources.map(resource => {
                if ('http://schema.org/ImageObject' === resource.id) {
                    resource.fields.map(field => {
                        if ('file' === field.name) {
                            field.denormalizeData = value => ({
                                src: value
                            });

                            field.normalizeData = value => {
                                if (value && value.rawFile instanceof File) {
                                    console.log("Instance file")
                                    const body = new FormData();
                                    body.append('file', value.rawFile);


                                    return fetch(`${entrypoint}/game_images`, {
                                        body,
                                        method: 'POST',
                                        headers: {
                                            Authorization: `Bearer ${window.localStorage.getItem("admin_token")}`,
                                        }
                                    })
                                        .then(response => {
                                            return response.json()
                                        });
                                }

                                return value.src;
                            };
                        }
                        return field;
                    });
                }
                return resource;
            });
            return {api};
        }),
        (result) => {
            switch (result.status) {
                case 401:
                    return Promise.resolve({
                        api: result.api,
                        customRoutes: [
                            <Route path="/" render={() => {
                                return window.localStorage.getItem("admin_token") ? window.location.reload() :
                                    <Redirect to="/login"/>
                            }}/>
                        ],
                    });

                default:
                    return Promise.reject(result);
            }
        },
    );

const dataProvider = baseHydraDataProvider(entrypoint, fetchHydra, apiDocumentationParser);


export default () => (
    <HydraAdmin
        dataProvider={dataProvider}
        authProvider={authProvider}
        entrypoint={entrypoint}
    >
        <ResourceGuesser name="users" list={UserList}/>
        <ResourceGuesser name="games" edit={GameEdit} create={GameCreate}/>
        <ResourceGuesser name="game_images" show={GameShow} list={GameImageList} create={GameImageCreate}
                         edit={GameImageUpdate}/>
    </HydraAdmin>
);

I'dont know if it's an issue or not but, i've got a first request which upload my file with respond code 201 so all is fine, but after a second request is making with the response of the upload request, i don't know why
really_strange

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.