UploaderJava
com.mediafire.sdk.uploader allows you to conduct a resumable upload. The package mainly consists of a "manager" which dictates the flow process of a resumable upload. Three classes and one interface are available which can be used for conducting a resumable upload:
- UploadItem
- UploadItemOptions
- UploadRunnable
- UploadListener
Uploader Components
UploadItem
UploadItem is a descriptor object used by the UploadRunnable. It represents an item to be uploaded. Initializing an UploadItem is simple if you have a path:
String path = "c:/images/deal_with_it_cat.gif";
UploadItem mfUploadItem = new UploadItem(path );
UploadItem mfUploadItem = new UploadItem(path );
An UploadItem can also be initialized using a customized set of options represented by the UploadItemOptions class:
String path = "c:/images/deal_with_it_cat.gif";
UploadItemOptions options = //... instantiate object
UploadItem mfUploadItem = new UploadItem(path , someOptions);
UploadItemOptions options = //... instantiate object
UploadItem mfUploadItem = new UploadItem(path , someOptions);
UploadItemOptions
Note: For a full understanding of the upload process, see the documentation regarding resumable uploads.
UploadItemOptions allows customization of certain decisions that must be made during the resumable upload process offered by the "manager" UploadRunnable". Options include:
- Specifying a custom filename to use.
- Specifying how to handle an upload when a hash already exists in the cloud, account, or in the folder where the item is being uploaded.
- Specifying a destination folder key.
- Specifying a destination path.
- Specifying version control.
Instantiating an UploadItemOptions is simple with the Builder pattern used:
UploadItemOptions.Builder builder = new UploadItemOptions.Builder();
builder.actionOnInAccount(ActionOnInAccount.UPLOAD_IF_NOT_IN_FOLDER);
builder.actionOnDuplicate(ActionOnDuplicate.KEEP);
builder.uploadPath("/deal_with_it_cat_gif_folder");
UploadItemOptions options = builder.build();
builder.actionOnInAccount(ActionOnInAccount.UPLOAD_IF_NOT_IN_FOLDER);
builder.actionOnDuplicate(ActionOnDuplicate.KEEP);
builder.uploadPath("/deal_with_it_cat_gif_folder");
UploadItemOptions options = builder.build();
Once an UploadItemOptions object is created, an UploadItem can be created using these options:
UploadItemOptions options = builder.build();
UploadItem mfUploadItem = new UploadItem("c:/images/deal_with_it_cat.gif", options);
UploadItem mfUploadItem = new UploadItem("c:/images/deal_with_it_cat.gif", options);
UploadListener
The interface UploadListener provides a callback throughout the upload process when using UploadRunnable. The methods in the UploadListener class are as follows:
public void onCancelled(UploadItem mfUploadItem);
public void onProgressUpdate(UploadItem mfUploadItem, int currentChunk, int totalChunks);
public void onPolling(UploadItem mfUploadItem, String message);
public void onStarted(UploadItem mfUploadItem);
public void onCompleted(UploadItem mfUploadItem);
public void onProgressUpdate(UploadItem mfUploadItem, int currentChunk, int totalChunks);
public void onPolling(UploadItem mfUploadItem, String message);
public void onStarted(UploadItem mfUploadItem);
public void onCompleted(UploadItem mfUploadItem);
Review the UploadRunnable class to understand when/where the interface methods are called.
UploadRunnable
UploadRunnable is a Runnable which can be used to facilitate easy resumable uploads. UploadRunnable object uses the Builder pattern for convenience.
MediaFire resumable uploads may require the use of several APIs:
Examples
Basic Resumable Upload
UploadItem mfUploadItem = //...
// create builder
UploadRunnable.Builder builder = new UploadRunnable.Builder(configuration, mfUploadItem);
// initialize UploadRunnable using builder
UploadRunnable mfUploadRunnable = builder.build();
// execute Runnable using Thread or use a ThreadPoolExecutor
// create builder
UploadRunnable.Builder builder = new UploadRunnable.Builder(configuration, mfUploadItem);
// initialize UploadRunnable using builder
UploadRunnable mfUploadRunnable = builder.build();
// execute Runnable using Thread or use a ThreadPoolExecutor
Using an UploadListener
UploadItem mfUploadItem = //...
UploadListener mfUploadListener = //...
// create builder
UploadRunnable.Builder builder = new MFUploadRunnable.Builder(config, mfUploadItem);
builder.uploadListener(mfUploadListener);
// initialize UploadRunnable using builder
UploadRunnable mfUploadRunnable = builder.build();
// execute Runnable using Thread or use a ThreadPoolExecutor
UploadListener mfUploadListener = //...
// create builder
UploadRunnable.Builder builder = new MFUploadRunnable.Builder(config, mfUploadItem);
builder.uploadListener(mfUploadListener);
// initialize UploadRunnable using builder
UploadRunnable mfUploadRunnable = builder.build();
// execute Runnable using Thread or use a ThreadPoolExecutor
Using All Options
UploadItem mfUploadItem = //...
UploadListener mfUploadListener = //...
// create builder
UploadRunnable.Builder builder = new UploadRunnable.Builder(config, mfUploadItem);
builder.maxPolls(3);
builder.millisecondsBetweenPolls(20000);
builder.maxUploadAttempts(2);
builder.uploadListener(mfUploadListener);
// initialize UploadRunnable using builder
UploadRunnable mfUploadRunnable = builder.build();
// execute Runnable using Thread or use a ThreadPoolExecutor
UploadListener mfUploadListener = //...
// create builder
UploadRunnable.Builder builder = new UploadRunnable.Builder(config, mfUploadItem);
builder.maxPolls(3);
builder.millisecondsBetweenPolls(20000);
builder.maxUploadAttempts(2);
builder.uploadListener(mfUploadListener);
// initialize UploadRunnable using builder
UploadRunnable mfUploadRunnable = builder.build();
// execute Runnable using Thread or use a ThreadPoolExecutor