emotality's avatar

Nova saves file as .bin file

I'm having the exact same issue as this person on GitHub, but with a .pdc type of file.

Upload a .pdc file and gets saved as a .bin

Any ideas? Thanks!

0 likes
4 replies
Screenbeetle's avatar

I've got a customer reporting the same issue but only occasionally - 3 cases out of a couple of hundred uploads and all in the last month. In case it helps someone spot a pattern my customer has so far reported 3 issues as follows:

  • 1 Mac Pages document saving as .bin
  • 1 MS Word for Mac saving as .bin
  • 1 PDF saving as a .zip (not quite sure if the user uploaded this by mistake but they swear they didn't :-)

I'll report back if I work out what's happening

1 like
Screenbeetle's avatar
Level 15

Hello again @emotality - we fixed our project.

For us it was a file upload issue outside of Nova but I think the two issues are the same.

When uploading a file the ->store() method uses Illuminate\Http\UploadedFile. By default this hashes the name and then tries to add back the extension by working out the mime type from the contents of the file itself - which doesn't always work. The docs explain it like this:

The UploadedFile class also contains methods for accessing the file's fully-qualified path and its extension. The extension method will attempt to guess the file's extension based on its contents. This extension may be different from the extension that was supplied by the client

https://laravel.com/docs/5.8/requests#files

The fix for us was to use the storeAs method instead and use the original extensions as part of the name:

->storeAs(
                'files',
                sha1(time()) . '.' . $file->getClientOriginalExtension()
            );

As Nova also uses Illuminate\Http\UploadedFile you should be able to add an override in your model to use storeAs in the same way - something like:

public function fields(Request $request)
{
        return [
          
            File::make('File')
                ->storeAs(function (Request $request) {
                    // something similar to the above
                })
        ];
}

I should credit @edoc for this as he mainly figured it out!

1 like
emotality's avatar

@SCREENBEETLE - It worked! Thanks so much, life saver :D

For the time being, I just downloaded the .bin file with a filename of example.pdc but your answer is the correct way of fixing it!

End results:

File::make('File', 'filepath')
    ->rules('required', 'file')
    ->disk('documents')
    ->storeAs(function(Request $request) {
        return sha1(time()) . '.' . $request->file('filepath')->getClientOriginalExtension();
    }),

Thanks @screenbeetle & @edoc ! <3

Please or to participate in this conversation.