duncanmcclean / guest-entries

Allow users to create, update & delete entries from the front-end of your site
https://statamic.com/addons/duncanmcclean/guest-entries
Other
3 stars 0 forks source link

Multiple files causing error (need to be able to upload multiple files) #10

Closed wm-simon closed 3 years ago

wm-simon commented 3 years ago

Description

I have a assets field for images. Guests should be able to upload multiple images to this field. When I save from the cp are the image paths are saved like this:

images:
  - path/filename.jpg
  - path/filename.jpg
  - path/filename.jpg

These are my input fields:

<input class="w-full" type="file" name="images[0]" accept="image/png, image/jpeg">
<input class="w-full" type="file" name="images[1]" accept="image/png, image/jpeg">
<input class="w-full" type="file" name="images[2]" accept="image/png, image/jpeg">

I've tried it with value too:

<input class="w-full" type="file" name="images[0][value]" accept="image/png, image/jpeg">
<input class="w-full" type="file" name="images[1][value]" accept="image/png, image/jpeg">
<input class="w-full" type="file" name="images[2][value]" accept="image/png, image/jpeg">

This error is shown:

Error
Call to a member function store() on array
domain.tld/!/guest-entries/create
        event(new GuestEntryDeleted($entry));

        return $this->withSuccess($request);

    }

    protected function uploadFile(string $key, Field $field, Request $request)

    {

        if (! isset($field->config()['container'])) {

            throw new AssetContainerNotFoundSpecified("Please specify an asset container on your [{$key}] field, in order for file uploads to work.");

        }

        /** @var \Statamic\Assets\AssetContainer $assetContainer */

        $assetContainer = AssetContainer::findByHandle($field->config()['container']);

        $path = '/' . $request->file($key)

            ->store(

                isset($field->config()['folder'])

                    ? $field->config()['folder']

                    : $assetContainer->diskPath(),

                $assetContainer->diskHandle()

            );

        if (isset($field->config()['max_items']) && $field->config()['max_items'] > 1) {

            return [str_replace($assetContainer->diskPath(), '', $path)];

        }

        return str_replace($assetContainer->diskPath(), '', $path);

    }

    protected function honeypotPassed(Request $request): ?bool

    {

Some other thoughts to the file upload. What will happen, when a guest uploads a file with an existing name or the same file multiple times? Do they overwrite the old file from an other guest? Perhaps they should get a timestamp in the filename or something. Or is it possible to change the save path from the files? This would be great, because when all guest save into the same directory, this could be really confusing, if you have a lot of entries. It would be great to have influence and add something like {{ year }}/{{month}}/{{slug}} to the save path. Perhaps it's possible to add a param to the input, like <input data-path="{{ year }}/{{month}}/{{slug}}"> .

Steps to reproduce

No response

Environment

Statamic 3.2.5 Pro Laravel 8.61.0 PHP 7.4.4 doublethreedigital/guest-entries 1.0.5

duncanmcclean commented 3 years ago

Multiple file uploads are not currently supported - was going to leave that for later if anyone ran into it (which you obviously have 😅 )

And with the filenames, I'll probably just make it timestamp-name.jpg or something like that. I don't think specifying paths like that will work unfortunately as the POST request doesn't provide any data-* attributes.

I'll take a look tomorrow night (I've not got any time tonight).

duncanmcclean commented 3 years ago

Fixed and released in v1.0.6


To upload multiple files, you can do something like this:

<input type="file" name="attachments[]">
<input type="file" name="attachments[]">
<input type="file" name="attachments[]">

I've also changed how filenames are generated. Essentially they'll look like this from now on: timestamp-filename.ext. I know it's not totally flexible but I'm not totally sure on the best way to make it configurable so this is how it'll be for the time being.

wm-simon commented 3 years ago

composer update is not updating to version 1.0.6 .

duncanmcclean commented 3 years ago

composer update is not updating to version 1.0.6 .

Does it give any error messages? What does your composer.json file look like?

wm-simon commented 3 years ago

No error messages. That updates worked without problems.

That's my composer:

{
    "name": "statamic/statamic",
    "description": "Statamic",
    "keywords": [
        "statamic",
        "cms",
        "flat file",
        "laravel"
    ],
    "type": "project",
    "require": {
        "php": "^7.3 || ^8.0",
        "doublethreedigital/guest-entries": "^1.0",
        "fideloper/proxy": "^4.2",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "jonassiewertsen/statamic-live-search": "^1.0",
        "laravel/framework": "^8.0",
        "laravel/tinker": "^2.0",
        "statamic/cms": "3.2.*"
    },
    "require-dev": {
        "barryvdh/laravel-debugbar": "^3.5",
        "facade/ignition": "^2.3.6",
        "fakerphp/faker": "^1.9.1",
        "mockery/mockery": "^1.3.1",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^9.3"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "pre-update-cmd": [
            "Statamic\\Console\\Composer\\Scripts::preUpdateCmd"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi",
            "@php artisan statamic:install --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}
wm-simon commented 3 years ago

Ah, found an error: Class DoubleThreeDigital\GuestEntries\Exceptions\AssetContainerNotFoundSpecified located in /vendor/doublethreedigital/guest-entries/src\Exceptions\AssetContainerNotSpecified.php does not comply with psr-4 autoloading standard. Skipping.

duncanmcclean commented 3 years ago

Ah cool, created an issue for that bug, I'll get it fixed tonight (see #11). Although, I don't think that error should be stopping you from installing the latest version....

If you run composer show |grep doublethreedigital/guest-entries, what is returned?

wm-simon commented 3 years ago

doublethreedigital/guest-entries v1.0.5 Perform CRUD operations in the front-end of your site.

duncanmcclean commented 3 years ago

Just released v1.0.7 with the fix for that previous error.

Could you try updating with composer update doublethreedigital/guest-entries?

wm-simon commented 3 years ago

Update worked. Saving entry with multiple files worked as well! Good job!

duncanmcclean commented 3 years ago

Fantastic, glad it worked!