Jump to content

Is there any way for processwire to save the assets in directories per year?


medcompanama
 Share

Recommended Posts

Hi All!

Currently Processwire saves the files in the assets directory as follows assets > files > pageid > jpg, doc, mp4, etc.

But what happens I have a project that we developed 2 years ago and currently uses 2 TB of space because the majority of content is video, so I thought that if Processwire could save the files per year, it means that the structure is thus assets > files > year > Pageid > jpg, doc, mp4, etc.

This in order to be able to create an NFS volume for each year and that the volume does not grow in an exaggerated way, since the bigger the volume, the longer the reading and writing times in that volume.

Thanks

Link to comment
Share on other sites

Reading and writing times shouldn't be (noticeably) influenced by the pure size of a volume (unless the physical disk underneath is nearing the end of its capacity or the underlying FS becomes highly fragmented). Enumerating large number of entries (files/subdirectories) in a single directory would likely be a factor, so if you have more than 10k pages, breaking up the flat hierarchy of site directories could be worth thinking about - but even then, structuring them by year wouldn't be my first approach as it is counter-intuitive and has a number of pitfalls (e.g. timezone changes, duplicate files when overwriting, code that assumes all files for a page can be found in the same directory).

Here's an example that adds a three-digit parent directory based on the page id that could be a starting point. It ignores the pageFilesSecure option, so be careful with it. You can look at PagefilesManager::___path and Pagefilesmanager::___url how they deal with that and other "magic". Of course, instead of that purely id-based dir, you can have the getPageSubdir function return anything you like. If you want to split your data between volumes, it could just return "old" for page ids < 5000 (or whatever the latest page id on the old volume is) and "new" for those above.

/*
 * Change files path, insert a three-digit directory above, left-padded
 * with zeroes. This way, there won't be more than 999 directories under
 * site/assets.
 *
 * Subdirectory count won't be an issue unless you cross the million pages
 * mark.
 */
wire()->addHookAfter("PagefilesManager::path", null, "modifyPath");

function modifyPath(HookEvent $event) {
	$p = $event->return;
	$p = preg_replace_callback('~^(.*/)([^/]+)/~', function($match) {
		return $match[1] . getPageSubdir($match[2]) . "/" . $match[2] . "/";
	}, $p);
	$event->return = $p;
}

wire()->addHookAfter("PagefilesManager::url", null, "modifyUrl");

function modifyUrl(HookEvent $event) {
	$url = wire('config')->urls->files . getPageSubdir($event->object->page->id) . "/" . $event->object->page->id . "/";
	$event->return = $url;
}

function getPageSubdir($id) {
	return str_pad(substr($id, 0, 2), 3, '0', STR_PAD_LEFT);
}

 

  • Like 6
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...