Skip to content

Commit

Permalink
[BKNDLSS-24972]: Add upload file method for Java sdk (#504)
Browse files Browse the repository at this point in the history
* [BKNDLSS-24972]: Add upload file method for Java sdk

* [BKNDLSS-24972]: Improve async method

Co-authored-by: Vladimir Yalovy <[email protected]>
  • Loading branch information
JoeSilentJoe and Vladimir Yalovy authored Jul 30, 2021
1 parent 6cf71af commit 720546b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/com/backendless/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,61 @@ static Files getInstance()
return instance;
}

public BackendlessFile upload( String urlToFile, String backendlessPath )
{
return upload( urlToFile, backendlessPath, false );
}

public BackendlessFile upload( String urlToFile, String backendlessPath, boolean overwrite )
{
if( urlToFile == null || urlToFile.isEmpty() )
throw new NullPointerException( ExceptionMessage.NULL_URL_TO_FILE );

if( backendlessPath == null )
throw new NullPointerException( ExceptionMessage.NULL_PATH );

final String resultURL = Invoker.invokeSync( FILE_MANAGER_SERVER_ALIAS, "upload", new Object[] { urlToFile, backendlessPath, overwrite } );
return new BackendlessFile( resultURL );
}

public void upload( String urlToFile, String backendlessPath, AsyncCallback<BackendlessFile> responder )
{
upload( urlToFile, backendlessPath, false, responder );
}

public void upload( String urlToFile, String backendlessPath, boolean overwrite, final AsyncCallback<BackendlessFile> responder )
{
try
{
if( urlToFile == null || urlToFile.isEmpty() )
throw new NullPointerException( ExceptionMessage.NULL_URL_TO_FILE );

if( backendlessPath == null )
throw new NullPointerException( ExceptionMessage.NULL_PATH );

Invoker.invokeAsync( FILE_MANAGER_SERVER_ALIAS, "upload", new Object[] { urlToFile, backendlessPath, overwrite }, new AsyncCallback<String>()
{
@Override
public void handleResponse( String response )
{
responder.handleResponse( new BackendlessFile( response ) );
}

@Override
public void handleFault( BackendlessFault fault )
{
if( responder != null )
responder.handleFault( fault );
}
} );
}
catch( Throwable e )
{
if( responder != null )
responder.handleFault( new BackendlessFault( e ) );
}
}

public BackendlessFile upload( File file, String path ) throws Exception
{
return upload( file, path, false );
Expand Down
1 change: 1 addition & 0 deletions src/com/backendless/exceptions/ExceptionMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class ExceptionMessage
public final static String NULL_EMPTY_TEMPLATE_NAME = "Template name can not be null or empty.";
public final static String NULL_EMAIL_ENVELOPE = "EmailEnvelope can not be null.";

public final static String NULL_URL_TO_FILE = "URL to file cannot be null.";
public final static String NULL_FILE = "File cannot be null.";
public final static String NULL_PATH = "File path cannot be null or empty.";
public final static String NULL_NAME = "File name cannot be null or empty.";
Expand Down

0 comments on commit 720546b

Please sign in to comment.