package org.csanchez.aws.glacier.actions;

import static org.csanchez.aws.glacier.utils.Check.notBlank;
import static org.csanchez.aws.glacier.utils.Check.notNull;

import java.io.File;
import java.util.concurrent.Callable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.glacier.AmazonGlacierClient;
import com.amazonaws.services.glacier.transfer.ArchiveTransferManager;
import com.amazonaws.services.glacier.transfer.UploadResult;

public class Upload implements Callable<String> {

    private static final Log LOG = LogFactory.getLog( Upload.class );

    private final AmazonGlacierClient client;
    private final AWSCredentials credentials;
    private final String vault;
    private final String archive;

    public Upload( AmazonGlacierClient client, AWSCredentials credentials, String vault, String archive ) {
        this.client = notNull( client );
        this.credentials = notNull( credentials );
        this.vault = notBlank( vault );
        this.archive = notBlank( archive );
    }

    public String call() throws Exception {
        try {
            LOG.info( "Starting upload of archive \"" + archive + "\" to vault \"" + vault + "\"" );
            ArchiveTransferManager atm = new ArchiveTransferManager( client, credentials );
            UploadResult result = atm.upload( vault, archive, new File( archive ) );
            String archiveId = result.getArchiveId();

            LOG.info( "Archive \"" + archive + "\" (" + archiveId + ") successfully uploaded to vault \"" + vault + "\"" );
            return archiveId;
        } catch ( Exception e ) {
            throw new RuntimeException( "Failed to upload archive \"" + archive + "\" to vault \"" + vault + "\"", e );
        }
    }

}

@@ -36,16 +36,86 @@
 tions;%0A%0A
+import static org.apache.commons.io.FileUtils.byteCountToDisplaySize;%0A
 import s
@@ -1238,16 +1238,76 @@
   try %7B%0A
+            File upload = new File( archive );%0A            %0A
         
@@ -1359,32 +1359,84 @@
  + archive + %22%5C%22
+ (%22 + byteCountToDisplaySize( upload.length() ) + %22)
  to vault %5C%22%22 + 
@@ -1443,32 +1443,33 @@
 vault + %22%5C%22%22 );%0A
+%0A
             Arch
@@ -1606,35 +1606,22 @@
 rchive, 
-new File( archive )
+upload
  );%0A    

Add file size of archive to log message when uploading