laravel / nova-issues

557 stars 34 forks source link

Nova showing stored name of file instead of original name of file #2923

Closed mddickens closed 4 years ago

mddickens commented 4 years ago

Description:

After uploading a file and storing the original name of the file, Nova displays the stored name of the file in the detail and form views. It correctly shows the original name of the file in the index view, but then incorrectly shows the stored name in the detail and form views.

Detailed steps to reproduce the issue on a fresh Nova installation:

Upload a file and view it in the various views

crynobone commented 4 years ago

File by default is not shown on index and I not sure if I can reproduce the issue.

Please provide an example to reproduce the issue.

mddickens commented 4 years ago

screen The above explains it clearly. When the file is uploaded, I store the original name (as shown above), but I get the stored name on the file upload button field. And this shows the renaming issue as well.

davidhemphill commented 4 years ago

Show us your Nova Resource's code, plz. :-)

mddickens commented 4 years ago

Here you go. Please note that I added the storeAs call after I posted this comment this morning and while it addressed the extension renaming issue, it did not change the display of the stored name.

<?php

namespace App\Nova;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\File;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Http\Requests\NovaRequest;

class PrintFile extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $priority = 20;
    public static $group = 'Shops';
    public static $tableStyle = 'tight';
    public static $model = \App\Models\PrintFile::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'id';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id', 'name', 'description'
    ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make(__('ID'), 'id')
                ->sortable(),
            BelongsTo::make('Shop'),
            Text::make('Name')
                ->exceptOnForms(),
            Text::make('Description'),
            File::make('Print File', 'file_path')
                ->storeOriginalName('name')
                ->storeSize('file_size')
                ->storeAs(function(Request $request) {
                    return sha1($request->file_path->getClientOriginalName()) . '.' . pathinfo($request->file_path->getClientOriginalName(), PATHINFO_EXTENSION);
                })
                ->deletable(false)
                ->hideFromIndex()
                ->disk('public')
                ->required()
                ->prunable(),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}
crynobone commented 4 years ago

Screenshot 2020-10-13 at 8 53 21 AM

From what I understand here, you expect that the original name to be preserved since you used storeOriginalName(), however this operation has been overridden when you combine that with storeAs(). You should be able to keep the original file name if you used

File::make('Print File', 'file_path')
                ->storeSize('file_size')
                ->storeAs(function(Request $request) {
                    return $request->file_path->getClientOriginalName() . '.' . pathinfo($request->file_path->getClientOriginalName(), PATHINFO_EXTENSION);
                })
                ->deletable(false)
                ->hideFromIndex()
                ->disk('public')
                ->required()
                ->prunable(),
TheOddler commented 3 years ago

I found that you can use the displayUsing function to show the original filename:

File::make('file_path')
    ->storeOriginalName('original_filename')
    ->displayUsing(function ($value) {
        return $this->original_filename;
    })

Just make sure the displayUsing function uses the same column as the original filename is being stored in.

alexanderchr commented 2 years ago

The solution above no longer works, looks like the file field has stopped respecting displayUsing.

crynobone commented 2 years ago

@alexanderchr submit new bug report with full reproducing code. We don't review or reopen closed issues.