lekoala / silverstripe-base

A base module for my SilverStripe projects
19 stars 3 forks source link

FilePondField show existing uploads? #2

Closed micschk closed 6 years ago

micschk commented 6 years ago

Just a quick check: FilePondField is working very well both in back-end & front-end. The only thing which seems to be missing is the display of existing/previous uploaded files (like UploadField does).

Is the display of previous uploads not yet implemented? Or should this be working (and am I doing something wrong)?

FilePond seems to be able to do this (references): https://github.com/pqina/filepond/issues/13 https://pqina.nl/filepond/docs/patterns/api/filepond-object/#setting-initial-files

micschk commented 6 years ago

OK, I couldn't find anything in the code setting the existing uploads so I've added it. If you like I can send a PR for this addition.

screen shot 2018-07-13 at 11 46 25
/**
     * Set initial values to FilePondField
     * See: https://pqina.nl/filepond/docs/patterns/api/filepond-object/#setting-initial-files
     * @TODO: (especially in front-end) existing file-IDs may be spoofed which may result in unauthorized attachment of files
     * 
     * @return array
     */
    public function getExistingUploadsData()
    {
        // Both Value() & dataValue() seem to return an array eg: ['Files' => [258, 259, 257]]
        $fileIDarray = $this->Value() ?: ['Files' => []];
        if(!isset($fileIDarray['Files']) || !count($fileIDarray['Files'])) return [];

        $existingUploads = [];
        foreach($fileIDarray['Files'] as $fileID) {
            if($file = File::get()->byID($fileID)){
                $existingUploads[] = [
                    // the server file reference
                    'source' => (int) $fileID,
                    // set type to local to indicate an already uploaded file
                    'options' => [
                        'type' => 'local',
                        // file information
                        'file' => [
                            'name' => $file->Name,
                            'size' => (int) $file->getAbsoluteSize(),
                            'type' => $file->getMimeType(),
                        ]
                    ]
                ];
            }
        }

        return $existingUploads;
    }

    public function Field($properties = array())
    {
        ...
        $config = [
            ...
            'files' => $this->getExistingUploadsData(),
        ];
        ...
    }
lekoala commented 6 years ago

Yes sure I'd be happy to merge a PR doing that :-)

I haven't done it so far because I'm using only Filepond in scenarios where I don't need displaying existing files, but it's most certainly something I'll need someday!

lekoala commented 6 years ago

merged into master