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

add media upload chuncked #228

Open
wants to merge 1 commit into
base: master
from
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+84 −0
Diff settings

Always

Just for now

@@ -244,6 +244,67 @@ public UploadedMedia uploadMedia(String fileName, InputStream image) throws Twit
, new HttpParameter[]{new HttpParameter("media", fileName, image)}).asJSONObject());
}

@Override
public UploadedMedia uploadMediaChunked(File mediaFile) throws TwitterException {
String url = conf.getUploadBaseURL() + "media/upload.json";
try{
JSONObject initResponse = post(url,
new HttpParameter("command","INIT"),
new HttpParameter("media_type","video/mp4"),
new HttpParameter("total_bytes",mediaFile.length())).asJSONObject();

final long mediaId = initResponse.getLong("media_id");
final long uploadLimit = 5000000;

int segment = (int) Math.ceil((double)mediaFile.length()/(double)uploadLimit);
for(int i=0;i<segment;i++){
InputStream is=null;
try {
FileInputStream fis = new FileInputStream(mediaFile);
if(fis.skip(uploadLimit * i)<0)throw new TwitterException("InputStream can't skip.");
is = new FilterInputStream(fis) {
private long left = uploadLimit;
@Override
public int available() throws IOException {
return (int) Math.min(in.available(), left);
}

@Override
public int read() throws IOException {
if (left == 0) return -1;
int result = in.read();
if (result != -1) --left;
return result;
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if (left == 0) return -1;
int result = in.read(b, off, (int) Math.min(len, left));
if (result != -1) left -= result;
return result;
}
};
post(url,
new HttpParameter("command", "APPEND"),
new HttpParameter("media_id", mediaId),
new HttpParameter("segment_index", i),
new HttpParameter("media", mediaFile.getPath(), is)
);
}finally {
if(is!=null){
is.close();
}
}
}
return new UploadedMedia( post(url,new HttpParameter("command", "FINALIZE"), new HttpParameter("media_id", mediaId) ).asJSONObject());
} catch (JSONException e) {
throw new TwitterException(e);
}catch (IOException e){
throw new TwitterException(e.getMessage(), e);
}
}

/* Search Resources */

@Override
@@ -181,4 +181,18 @@
* @since Twitter4J 4.0.3
*/
UploadedMedia uploadMedia(String fileName, InputStream media) throws TwitterException;


/**
* Uploads media image to be attached via {@link #updateStatus(twitter4j.StatusUpdate)}
* <br>This method calls https://api.twitter.com/1.1/media/upload.json
*
* @param mediaFile the latest status to be updated.
* @return upload result
* @throws TwitterException when Twitter service or network is unavailable
* @see <a href="https://dev.twitter.com/docs/api/1.1/post/statuses/update">POST statuses/update | Twitter Developers</a>
* @see <a href="https://dev.twitter.com/docs/api/multiple-media-extended-entities">Multiple Media Entities in Statuses</a>
* @since Twitter4J 4.0.5
*/
UploadedMedia uploadMediaChunked(File mediaFile) throws TwitterException;
}
@@ -122,6 +122,15 @@ public void testUploadMediaByStream() throws Exception {
assertNotNull(media2.getSize());
}

public void testUploadMediaChunked() throws Exception{
String file = "src/test/resources/t4j-movie.mp4";
File mp4File = new File(file);
if (!mp4File.exists())mp4File = new File("twitter4j-core/" + file);
UploadedMedia media = twitter1.uploadMediaChunked(mp4File);
assertNotNull(media.getMediaId());
assertNotNull(media.getSize());
}

public void testRetweetMethods() throws Exception {
List<Status> statuses;

Binary file not shown.
ProTip! Use n and p to navigate between commits in a pull request.