-
-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for uploading a
ParseFile
from a URI (#1207)
- Loading branch information
Showing
10 changed files
with
510 additions
and
6 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
parse/src/main/java/com/parse/ParseCountingUriHttpBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2015-present, Parse, LLC. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
package com.parse; | ||
|
||
import android.net.Uri; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
class ParseCountingUriHttpBody extends ParseUriHttpBody { | ||
|
||
private static final int DEFAULT_CHUNK_SIZE = 4096; | ||
private static final int EOF = -1; | ||
|
||
private final ProgressCallback progressCallback; | ||
|
||
public ParseCountingUriHttpBody(Uri uri, ProgressCallback progressCallback) { | ||
this(uri, null, progressCallback); | ||
} | ||
|
||
public ParseCountingUriHttpBody( | ||
Uri uri, String contentType, ProgressCallback progressCallback) { | ||
super(uri, contentType); | ||
this.progressCallback = progressCallback; | ||
} | ||
|
||
@Override | ||
public void writeTo(OutputStream output) throws IOException { | ||
if (output == null) { | ||
throw new IllegalArgumentException("Output stream may not be null"); | ||
} | ||
|
||
final InputStream fileInput = | ||
Parse.getApplicationContext().getContentResolver().openInputStream(uri); | ||
try { | ||
byte[] buffer = new byte[DEFAULT_CHUNK_SIZE]; | ||
int n; | ||
long totalLength = getContentLength(); | ||
long position = 0; | ||
while (EOF != (n = fileInput.read(buffer))) { | ||
output.write(buffer, 0, n); | ||
position += n; | ||
|
||
if (progressCallback != null) { | ||
int progress = (int) (100 * position / totalLength); | ||
progressCallback.done(progress); | ||
} | ||
} | ||
} finally { | ||
ParseIOUtils.closeQuietly(fileInput); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) 2015-present, Parse, LLC. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
package com.parse; | ||
|
||
import static com.parse.Parse.getApplicationContext; | ||
|
||
import android.content.res.AssetFileDescriptor; | ||
import android.database.Cursor; | ||
import android.net.Uri; | ||
import android.os.ParcelFileDescriptor; | ||
import android.provider.OpenableColumns; | ||
import com.parse.http.ParseHttpBody; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
class ParseUriHttpBody extends ParseHttpBody { | ||
|
||
/* package */ final Uri uri; | ||
|
||
public ParseUriHttpBody(Uri uri) { | ||
this(uri, null); | ||
} | ||
|
||
public ParseUriHttpBody(Uri uri, String contentType) { | ||
super(contentType, getUriLength(uri)); | ||
this.uri = uri; | ||
} | ||
|
||
private static long getUriLength(Uri uri) { | ||
long length = -1; | ||
|
||
try (Cursor cursor = | ||
getApplicationContext() | ||
.getContentResolver() | ||
.query(uri, null, null, null, null, null)) { | ||
if (cursor != null && cursor.moveToFirst()) { | ||
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE); | ||
if (!cursor.isNull(sizeIndex)) { | ||
length = cursor.getLong(sizeIndex); | ||
} | ||
} | ||
} | ||
if (length == -1) { | ||
try { | ||
ParcelFileDescriptor parcelFileDescriptor = | ||
getApplicationContext().getContentResolver().openFileDescriptor(uri, "r"); | ||
if (parcelFileDescriptor != null) { | ||
length = parcelFileDescriptor.getStatSize(); | ||
parcelFileDescriptor.close(); | ||
} | ||
} catch (IOException ignored) { | ||
} | ||
} | ||
if (length == -1) { | ||
try { | ||
AssetFileDescriptor assetFileDescriptor = | ||
getApplicationContext() | ||
.getContentResolver() | ||
.openAssetFileDescriptor(uri, "r"); | ||
if (assetFileDescriptor != null) { | ||
length = assetFileDescriptor.getLength(); | ||
assetFileDescriptor.close(); | ||
} | ||
} catch (IOException ignored) { | ||
} | ||
} | ||
return length; | ||
} | ||
|
||
@Override | ||
public InputStream getContent() throws IOException { | ||
return getApplicationContext().getContentResolver().openInputStream(uri); | ||
} | ||
|
||
@Override | ||
public void writeTo(OutputStream out) throws IOException { | ||
if (out == null) { | ||
throw new IllegalArgumentException("Output stream can not be null"); | ||
} | ||
|
||
final InputStream fileInput = | ||
getApplicationContext().getContentResolver().openInputStream(uri); | ||
try { | ||
ParseIOUtils.copy(fileInput, out); | ||
} finally { | ||
ParseIOUtils.closeQuietly(fileInput); | ||
} | ||
} | ||
} |
Oops, something went wrong.