Storing audio files elsewhere

I have a site that has a lot of hour long mp3 files. After years of adding about one a week, the size of the site is getting quite large. In the past, I’ve shuffled all these audio files off to a different storage location with files named according to date, and I’m in the process of moving them all out again, only this time naming them by date and slug for more flexibility and reliability in automatic linking.

This works well enough, and dramatically reduces the size of my repository, meaning I can actually have the site files on github (the site was too large before), and that I can actually deploy the whole site to a new server without git crapping out because it’s too big.

However, I’ve become attached to uploading the mp3s in the control panel, so what I would like to be able to do is upload a file in the control panel, and have Kirby dump that file to my object storage service, and then delete it from Kirby.

So my question is, does anybody have any info about how I would get started on that? I haven’t dug into the CDN plugin yet, and I know that doesn’t delete the original file from Kirby, but could I possibly modify it for that purpose?

Maybe use a post hook to do the heavy lifting?

First, set up a script on your media server that can download a file from your site server. Then set up a hook like this?

kirby()->hook('panel.file.upload', function($file) {

  // 1. Send the uploaded filename to media server, store the result
  $mediaUrl = file_get_contents('http://mediaserver.com?download='.urlencode($file->url()));

  // 2. Store the new filename in your page's content file
  $page->update(['mediaUrl' => $mediaUrl]);

  // 3. Delete the file
  $file->delete();
});
1 Like

Hmmm…that’s a step in the right direction. I can’t easily set up a script on my media server, since it’s just object storage (and I don’t want to have to spin up compute on it every time I want to pull a file over).

However, I could possibly use this method to run my terminal command that uploads files. I wonder if I could rename the file at the same time. The files are named by date and slug, so if I can set it to rename it when it’s uploaded, I don’t have to manually do all that.

I’ll explore this route, thanks!

You could run any shell command from your hooks, but please note that the hook will be blocking the Panel frontend (the loading bar won’t finish until the file is done uploading). You could do the uploading the background though (e.g. using a cronjob that checks if any new files were uploaded).

I’m having trouble getting the upload working, but I think what I’m going to do is just have Kirby rename the file on adding it to the panel, and then script the dumping to the media server elsewhere for now.