Virtual Files for page?

There’s another way to handle that: Spacer GIF :slight_smile: Just kidding! Almost.

Use a file create hook to upload the file to azure or whatever and once the upload succeeded, replace the actual file on the disk with a 1x1 px version. I have done this a couple times in client projects and it works pretty great. You can store additional meta data in the text file. For example the original dimensions or a reference id of the uploaded file.

Another way is really working with virtual file objects. There’s a Files::factory() method that’s comparable to the Pages::factory() method. You can pass a set of file props to that method with an array.

class CustomPage extends Page 
{

    public function files()
    {

        $files = [
            [
                'filename' => 'a.jpg',
                'url'      => 'https://cdn.somewhere.com/a.jpg',
            ],
            [
                'filename' => 'b.jpg',
                'url'      => 'https://cdn.somewhere.com/b.jpg',
            ]
        ];

        return Files::factory($files, $this);

    }

}

The only problem with this is that you can’t use the File object methods that rely on reading the file. I.e. file size, dimensions, etc.

For that you would need to create your very own files collection with your own file objects


class CustomFile extends File
{
  // overwrite file methods here
}

class CustomPage extends Page 
{

    public function files()
    {
        $files = new Files([], $this);
    
        $a = new CustomFile([
            'filename' => 'a.jpg',
            'parent'   => $this,
            'url'      => 'https://cdn.somewhere.com/a.jpg',
        ]);

        $b = new CustomFile([
            'filename' => 'b.jpg',
            'parent'   => $this,
            'url'      => 'https://cdn.somewhere.com/b.jpg',
        ]);

        $files->append($a->id(), $a);
        $files->append($b->id(), $b);

        return $files;

    }

}

I haven’t tested this, but in theory it should work :slight_smile:

7 Likes