Skip to content

Random code cleanup for Storage #758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion firebase-storage/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ package com.google.firebase.storage {
method @com.google.firebase.storage.StorageException.ErrorCode public int getErrorCode();
method public int getHttpResultCode();
method public boolean getIsRecoverableException();
method @NonNull public String getMessage();
field public static final int ERROR_BUCKET_NOT_FOUND = -13011; // 0xffffcd2d
field public static final int ERROR_CANCELED = -13040; // 0xffffcd10
field public static final int ERROR_INVALID_CHECKSUM = -13031; // 0xffffcd19
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ public void updateMetadata() throws ExecutionException, InterruptedException {

@Test
public void pagedListFiles() throws ExecutionException, InterruptedException {
Task<ListResult> listTask = storageClient.getReference(randomPrefix).list(2);
Task<ListResult> listTask = getReference().list(2);
ListResult listResult = Tasks.await(listTask);

assertThat(listResult.getItems())
.containsExactly(getReference("download.dat"), getReference("metadata.dat"));
assertThat(listResult.getPrefixes()).isEmpty();
assertThat(listResult.getPageToken()).isNotEmpty();

listTask = storageClient.getReference(randomPrefix).list(2, listResult.getPageToken());
listTask = getReference().list(2, listResult.getPageToken());
listResult = Tasks.await(listTask);

assertThat(listResult.getItems()).isEmpty();
Expand All @@ -152,7 +152,7 @@ public void pagedListFiles() throws ExecutionException, InterruptedException {

@Test
public void listAllFiles() throws ExecutionException, InterruptedException {
Task<ListResult> listTask = storageClient.getReference(randomPrefix).listAll();
Task<ListResult> listTask = getReference().listAll();
ListResult listResult = Tasks.await(listTask);

assertThat(listResult.getPrefixes()).containsExactly(getReference("prefix"));
Expand All @@ -161,6 +161,11 @@ public void listAllFiles() throws ExecutionException, InterruptedException {
assertThat(listResult.getPageToken()).isNull();
}

@NonNull
private StorageReference getReference() {
return storageClient.getReference(randomPrefix);
}

@NonNull
private StorageReference getReference(String filename) {
return storageClient.getReference(randomPrefix + "/" + filename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public List<StorageReference> getItems() {
}

/**
* Returns a token that can be used to resume a previous {@code list()} operation. ${@code null}
* Returns a token that can be used to resume a previous {@code list()} operation. {@code null}
* indicates that there are no more results.
*
* @return A page token if more results are available.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ public class StorageException extends FirebaseException {

private final int errorCode;
private final int httpResultCode;
private String detailMessage;
private Throwable cause;

StorageException(@ErrorCode int errorCode, Throwable inner, int httpResultCode) {
this.detailMessage = getErrorMessageForCode(errorCode);
super(getErrorMessageForCode(errorCode));

this.cause = inner;
this.errorCode = errorCode;
this.httpResultCode = httpResultCode;
Log.e(
TAG,
"StorageException has occurred.\n"
+ detailMessage
+ getErrorMessageForCode(errorCode)
+ "\n Code: "
+ Integer.toString(this.errorCode)
+ this.errorCode
+ " HttpResult: "
+ Integer.toString(this.httpResultCode));
+ this.httpResultCode);
if (cause != null) {
Log.e(TAG, cause.getMessage(), cause);
}
Expand Down Expand Up @@ -155,16 +155,6 @@ static String getErrorMessageForCode(int errorCode) {
}
}

/**
* Returns the detail message which was provided when this {@code Throwable} was created. Returns
* {@code null} if no message was provided at creation time.
*/
@NonNull
@Override
public String getMessage() {
return detailMessage;
}

/** Returns the cause of this {@code Throwable}, or {@code null} if there is no cause. */
@Nullable
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.common.internal.Preconditions;
import com.google.firebase.storage.internal.Slashes;
import com.google.firebase.storage.internal.Util;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -256,18 +254,12 @@ public StorageReference getReference() {
if (TextUtils.isEmpty(bucket) || TextUtils.isEmpty(path)) {
return null;
}
Uri uri;
try {
uri =
new Uri.Builder()
.scheme("gs")
.authority(bucket)
.encodedPath(Slashes.preserveSlashEncode(path))
.build();
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unable to create a valid default Uri. " + bucket + path, e);
throw new IllegalStateException(e);
}
Uri uri =
new Uri.Builder()
.scheme("gs")
.authority(bucket)
.encodedPath(Slashes.preserveSlashEncode(path))
.build();

return new StorageReference(uri, mStorage);
}
Expand All @@ -276,7 +268,7 @@ public StorageReference getReference() {
}

@NonNull
JSONObject createJSONObject() throws JSONException {
JSONObject createJSONObject() {
Map<String, Object> jsonData = new HashMap<>();

if (mContentType.isUserProvided()) {
Expand Down Expand Up @@ -347,11 +339,11 @@ private void parseJSON(JSONObject jsonObject) throws JSONException {
mMetadata.mGeneration = jsonObject.optString(GENERATION_KEY);
mMetadata.mPath = jsonObject.optString(NAME_KEY);
mMetadata.mBucket = jsonObject.optString(BUCKET_KEY);
mMetadata.mMetadataGeneration = (jsonObject.optString(META_GENERATION_KEY));
mMetadata.mCreationTime = (jsonObject.optString(TIME_CREATED_KEY));
mMetadata.mUpdatedTime = (jsonObject.optString(TIME_UPDATED_KEY));
mMetadata.mSize = (jsonObject.optLong(SIZE_KEY));
mMetadata.mMD5Hash = (jsonObject.optString(MD5_HASH_KEY));
mMetadata.mMetadataGeneration = jsonObject.optString(META_GENERATION_KEY);
mMetadata.mCreationTime = jsonObject.optString(TIME_CREATED_KEY);
mMetadata.mUpdatedTime = jsonObject.optString(TIME_UPDATED_KEY);
mMetadata.mSize = jsonObject.optLong(SIZE_KEY);
mMetadata.mMD5Hash = jsonObject.optString(MD5_HASH_KEY);

if (jsonObject.has(CUSTOM_METADATA_KEY) && !jsonObject.isNull(CUSTOM_METADATA_KEY)) {
JSONObject customMetadata = jsonObject.getJSONObject(CUSTOM_METADATA_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -85,18 +84,8 @@ public StorageReference child(@NonNull String pathString) {
!TextUtils.isEmpty(pathString), "childName cannot be null or empty");

pathString = Slashes.normalizeSlashes(pathString);
Uri child;
try {
child =
mStorageUri
.buildUpon()
.appendEncodedPath(Slashes.preserveSlashEncode(pathString))
.build();
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unable to create a valid default Uri. " + pathString, e);

throw new IllegalArgumentException("childName");
}
Uri child =
mStorageUri.buildUpon().appendEncodedPath(Slashes.preserveSlashEncode(pathString)).build();
return new StorageReference(child, mFirebaseStorage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,9 @@ public UpdateMetadataTask(

@Override
public void run() {
final NetworkRequest request;
try {
request =
new UpdateMetadataNetworkRequest(
mStorageRef.getStorageUri(), mStorageRef.getApp(), mNewMetadata.createJSONObject());
} catch (final JSONException e) {
Log.e(TAG, "Unable to create the request from metadata.", e);

mPendingResult.setException(StorageException.fromException(e));
return;
}
final NetworkRequest request =
new UpdateMetadataNetworkRequest(
mStorageRef.getStorageUri(), mStorageRef.getApp(), mNewMetadata.createJSONObject());

mSender.sendWithExponentialBackoff(request);
if (request.isResultSuccess()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,12 @@ private void beginResumableUpload() {
if (TextUtils.isEmpty(mimeType)) {
mimeType = APPLICATION_OCTET_STREAM;
}
NetworkRequest startRequest;
try {
startRequest =
new ResumableUploadStartRequest(
mStorageRef.getStorageUri(),
mStorageRef.getApp(),
mMetadata != null ? mMetadata.createJSONObject() : null,
mimeType);
} catch (JSONException e) {
Log.e(TAG, "Unable to create a network request from metadata", e);
mException = e;
return;
}
NetworkRequest startRequest =
new ResumableUploadStartRequest(
mStorageRef.getStorageUri(),
mStorageRef.getApp(),
mMetadata != null ? mMetadata.createJSONObject() : null,
mimeType);

if (!sendWithRetry(startRequest)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.common.internal.Preconditions;
import java.io.UnsupportedEncodingException;

/**
* Utility methods for Firebase Storage.
Expand All @@ -34,10 +33,9 @@ public class Slashes {
*
* @param s The String to convert
* @return A partially URL encoded string where slashes are preserved.
* @throws UnsupportedEncodingException
*/
@NonNull
public static String preserveSlashEncode(@Nullable String s) throws UnsupportedEncodingException {
public static String preserveSlashEncode(@Nullable String s) {
if (TextUtils.isEmpty(s)) {
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import android.net.Uri;
import androidx.annotation.NonNull;
import com.google.firebase.FirebaseApp;
import java.io.UnsupportedEncodingException;
import java.util.Collections;

/** A network request that returns bytes of a gcs object. */
Expand All @@ -40,7 +39,7 @@ protected String getAction() {

@Override
@NonNull
protected String getQueryParameters() throws UnsupportedEncodingException {
protected String getQueryParameters() {
return getPostDataString(
Collections.singletonList("alt"), Collections.singletonList("media"), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.firebase.FirebaseApp;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -52,7 +51,7 @@ protected String getURL() {

@Override
@Nullable
protected String getQueryParameters() throws UnsupportedEncodingException {
protected String getQueryParameters() {
List<String> keys = new ArrayList<>();
List<String> values = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -196,7 +194,7 @@ protected int getOutputRawSize() {
* @return query parameters in string form.
*/
@Nullable
protected String getQueryParameters() throws UnsupportedEncodingException {
protected String getQueryParameters() {
return null;
}

Expand Down Expand Up @@ -508,8 +506,7 @@ public boolean isResultSuccess() {
return resultCode >= 200 && resultCode < 300;
}

String getPostDataString(@Nullable List<String> keys, List<String> values, boolean encode)
throws UnsupportedEncodingException {
String getPostDataString(@Nullable List<String> keys, List<String> values, boolean encode) {
if (keys == null || keys.size() == 0) {
return null;
}
Expand All @@ -523,9 +520,9 @@ String getPostDataString(@Nullable List<String> keys, List<String> values, boole
result.append("&");
}

result.append(encode ? URLEncoder.encode(keys.get(i), "UTF-8") : keys.get(i));
result.append(encode ? Uri.encode(keys.get(i), "UTF-8") : keys.get(i));
result.append("=");
result.append(encode ? URLEncoder.encode(values.get(i), "UTF-8") : values.get(i));
result.append(encode ? Uri.encode(values.get(i), "UTF-8") : values.get(i));
}

return result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import androidx.annotation.Nullable;
import com.google.firebase.FirebaseApp;
import com.google.firebase.storage.internal.Slashes;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
Expand Down Expand Up @@ -60,7 +59,7 @@ protected String getAction() {

@Override
@NonNull
protected String getQueryParameters() throws UnsupportedEncodingException {
protected String getQueryParameters() {
List<String> keys = new ArrayList<>();
List<String> values = new ArrayList<>();

Expand Down