Commit 8e3297c8 authored by Joe Hoyle's avatar Joe Hoyle
Browse files

Added upload-directory subcommand with better sync functionality

parent 7f63bb86
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
<?php

class S3_Uploads_ChangedFilesIterator extends Aws\S3\Sync\ChangedFilesIterator {

	public function accept() {

		$current = $this->current();

		$key = $this->sourceConverter->convert((string) $current);
		if (!($data = $this->getTargetData($key))) {
			return true;
		}

		// Ensure it hasn't been modified since the mtime
		return $current->getMTime() > $data[1];
	}
}
 No newline at end of file
+40 −0
Original line number Diff line number Diff line
<?php

class S3_Uploads_UploadSyncBuilder extends Aws\S3\Sync\UploadSyncBuilder {

	/**
     * Builds a UploadSync or DownloadSync object
     *
     * @return AbstractSync
     */
    public function build()
    {
        $this->validateRequirements();
        $this->sourceConverter = $this->sourceConverter ?: $this->getDefaultSourceConverter();
        $this->targetConverter = $this->targetConverter ?: $this->getDefaultTargetConverter();

        // Only wrap the source iterator in a changed files iterator if we are not forcing the transfers
        if (!$this->forcing) {
            $this->sourceIterator->rewind();
            $this->sourceIterator = new S3_Uploads_ChangedFilesIterator(
                new \NoRewindIterator($this->sourceIterator),
                $this->getTargetIterator(),
                $this->sourceConverter,
                $this->targetConverter
            );
            $this->sourceIterator->rewind();
        }

        $sync = $this->specificBuild();

        if ($this->params) {
            $this->addCustomParamListener($sync);
        }

        if ($this->debug) {
            $this->addDebugListener($sync, is_bool($this->debug) ? STDOUT : $this->debug);
        }

        return $sync;
    }
}
 No newline at end of file
+42 −0
Original line number Diff line number Diff line
@@ -229,6 +229,48 @@ class S3_Uploads_WP_CLI_Command extends WP_CLI_Command {
		WP_CLI::success( sprintf( 'Completed copy from %s to %s', $from, $to ) );
	}

	/**
	 * Upload a directory to S3
	 * 
	 * @subcommand upload-directory
	 * @synopsis <from> [<to>] [--sync]
	 */
	public function upload_directory( $args, $args_assoc ) {

		$from = $args[0];
		$to = '';
		if ( isset( $args[1] ) ) {
			$to = $args[1];
		}

		$s3 = S3_Uploads::get_instance()->s3();
		$bucket = strtok( S3_UPLOADS_BUCKET, '/' );
		$prefix = '';

		if ( strpos( S3_UPLOADS_BUCKET, '/' ) ) {
			$prefix = trailingslashit( str_replace( strtok( S3_UPLOADS_BUCKET, '/' ) . '/', '', S3_UPLOADS_BUCKET ) );
		}

		require_once dirname( __FILE__ ) . '/class-s3-uploads-uploadsyncbuilder.php';
		require_once dirname( __FILE__ ) . '/class-s3-uploads-changed-files-iterator.php';

		try {
			$s3->uploadDirectory( 
				$from, 
				$bucket, 
				$prefix . $to, 
				array(
					'debug' => true, 
					'params' => array( 'ACL' => 'public-read' ),
					'builder' => new S3_Uploads_UploadSyncBuilder(),
					'force' => empty( $args_assoc['sync'] )
					) 
				); 
		} catch( Exception $e ) { 
			WP_CLI::error( $e->getMessage() );
		}
	}

	/**
	 * Delete files from S3
	 *