Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit de69cca

Browse files
authored
Merge pull request #865 from TwilioDevEd/update-conversations-retrieve-media
Update conversations retrieve media for mobile
2 parents 346921f + 4ba5077 commit de69cca

File tree

3 files changed

+23
-82
lines changed

3 files changed

+23
-82
lines changed
Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,8 @@
11
if (message.hasMedia()) {
2-
Message.Media media = message.getMedia();
3-
4-
String sid = media.getSid();
5-
String type = media.getType();
6-
String fn = media.getFileName();
7-
long size = media.getSize();
8-
9-
Timber.d("This is a media message with SID "+sid+", type "+type+", name "+fn+", and size "+size);
10-
11-
if (type.contentEquals("text/plain")) {
12-
final ByteArrayOutputStream out = new ByteArrayOutputStream();
13-
media.download(out, new StatusListener() {
14-
@Override
15-
public void onSuccess() {
16-
String content = out.toString();
17-
Timber.d("Downloaded media "+content);
18-
}
19-
20-
@Override
21-
public void onError(ErrorInfo error) {
22-
Timber.e("Error downloading media");
23-
}
24-
}, new ProgressListener() {
25-
@Override
26-
public void onStarted() {
27-
Timber.d("Download started");
28-
}
29-
30-
@Override
31-
public void onProgress(long bytes) {
32-
Timber.d("Downloaded "+bytes+" bytes");
33-
}
34-
35-
@Override
36-
public void onCompleted(String mediaSid) {
37-
Timber.d("Download completed");
38-
}
39-
});
40-
}
41-
}
2+
message.getMediaContentTemporaryUrl(new CallbackListener<String>() {
3+
@Override
4+
public void onSuccess(String mediaContentUrl) {
5+
Log.d("TAG", mediaContentUrl);
6+
}
7+
});
8+
}
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
// Set up output stream for media content
2-
NSString *tempFilename = [NSTemporaryDirectory() stringByAppendingPathComponent:message.mediaFilename ? @"file.dat" : message.mediaFilename];
3-
NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:tempFilename append:NO];
4-
5-
// Request the start of the download
6-
[message getMediaWithOutputStream:outputStream
7-
onStarted:^{
8-
// Called when download of media begins.
9-
} onProgress:^(NSUInteger bytes) {
10-
// Called as download progresses, with the current byte count.
11-
} onCompleted:^(NSString * _Nonnull mediaSid) {
12-
// Called when download is completed, with the new mediaSid if successful.
13-
// Full failure details will be provided through the completion block below.
14-
} completion:^(TCHResult * _Nonnull result) {
15-
if (!result.isSuccessful) {
16-
NSLog(@"Download failed: %@", result.error);
17-
} else {
18-
NSLog(@"Download successful");
19-
}
20-
}];
1+
if (message.hasMedia) {
2+
[message getMediaContentTemporaryUrlWithCompletion:^(TCHResult * _Nonnull result,
3+
NSString * _Nullable mediaContentURL) {
4+
if (result.isSuccessful) {
5+
// Use the url to download an image or other media
6+
NSLog(@"%@", mediaContentURL);
7+
}
8+
}];
9+
}
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
1-
// Set up output stream for media content
2-
let tempFilename = (NSTemporaryDirectory() as NSString).appendingPathComponent(message.mediaFilename ?? "file.dat")
3-
let outputStream = OutputStream(toFileAtPath: tempFilename, append: false)
4-
5-
// Request the start of the download
6-
if let outputStream = outputStream {
7-
message.getMediaWith(outputStream,
8-
onStarted: {
9-
// Called when download of media begins.
10-
},
11-
onProgress: { (bytes) in
12-
// Called as download progresses, with the current byte count.
13-
},
14-
onCompleted: { (mediaSid) in
15-
// Called when download is completed, with the new mediaSid if successful.
16-
// Full failure details will be provided through the completion block below.
17-
}) { (result) in
18-
if !result.isSuccessful() {
19-
print("Download failed: \(String(describing: result.error))")
20-
} else {
21-
print("Download successful")
1+
if message.hasMedia() {
2+
message.getMediaContentTemporaryUrl { (result, mediaContentUrl) in
3+
guard let mediaContentUrl = mediaContentUrl else {
4+
return
225
}
6+
// Use the url to download an image or other media
7+
print(mediaContentUrl)
238
}
24-
}
9+
}

0 commit comments

Comments
 (0)