Skip to content

Instantly share code, notes, and snippets.

@bdunogier
Created December 29, 2010 13:01
Show Gist options
  • Save bdunogier/758509 to your computer and use it in GitHub Desktop.
Save bdunogier/758509 to your computer and use it in GitHub Desktop.
Shards binary files in ezpublish storage directory to avoid running out of iNodes
#!/usr/bin/env php
<?php
/**
* File containing the shardfiles.php script
*
* @copyright Copyright (C) 1999-2010 eZ Systems AS. All rights reserved.
* @license http://ez.no/licenses/gnu_gpl GNU GPL v2
* @version //autogentag//
* @package
*/
/**
* This script updates the path to binary files and moves them to subfolders.
* Such a structure prevents from running out of i-nodes when too many binary files are stored
*/
require 'autoload.php';
$cli = eZCLI::instance();
$script = eZScript::instance( array( 'description' => "This script updates the path to binary files and moves them to subfolders",
'use-session' => false,
'use-modules' => false,
'use-extensions' => false ) );
$script->startup();
$options = $script->getOptions(
// options definition
"[n|dry-run:]",
// arguments definition
"",
// options documentation
array( 'dry-run' => 'Don\'t rename files, only prompt the operations' ) );
$sys = eZSys::instance();
$script->initialize();
define( 'OPT_DRY_RUN', ( $options['dry-run'] === true ) );
$db = eZDB::instance();
$cli->output( "Updating binary files path:");
$rows = $db->arrayQuery('select filename, mime_type from ezbinaryfile' );
$fileHandler = eZClusterFileHandler::instance();
foreach( $rows as $row )
{
$currentFilePath = filePathForBinaryFile( $row['filename'] , $row['mime_type'], 'old' );
$newFilePath = filePathForBinaryFile( $row['filename'] , $row['mime_type'], 'new' );
$cli->output( "- $currentFilePath => $newFilePath" );
if ( !file_exists( $newFilePath) )
{
if ( OPT_DRY_RUN === false )
{
$res = $fileHandler->fileMove( $currentFilePath, $newFilePath );
}
}
else
{
$cli->output(" ignoring already sharded file" );
}
}
$script->shutdown();
/**
* Generated the path to a binary file out of it's name
*
* @param string $fileName The filename (hash)
* @param string $mimeType The file's mimetype (application, image...)
* @param string $format Naming format, either old or new
* @return string
*/
function filePathForBinaryFile( $fileName, $mimeType, $format )
{
$storageDir = eZSys::storageDirectory();
list( $group, $type ) = explode( '/', $mimeType );
if ( $format == 'new' )
$filePath = "{$storageDir}/original/{$group}/" . substr( $fileName, 0, 3 ). "/{$fileName}";
elseif ( $format == 'old' )
$filePath = "$storageDir/original/$group/$fileName";
return $filePath;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment