Compare commits
3 Commits
c226691762
...
51a2b89af1
Author | SHA1 | Date | |
---|---|---|---|
51a2b89af1 | |||
54e87049b3 | |||
c4dc20d070 |
@ -3,6 +3,9 @@
|
||||
package="net.monarchpass.piecannon"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application android:label="@string/app_name">
|
||||
<activity android:name=".Main"
|
||||
android:label="@string/app_name">
|
||||
|
@ -4,18 +4,91 @@ import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.widget.TextView;
|
||||
import android.content.ContentResolver;
|
||||
|
||||
import com.google.common.io.ByteSource;
|
||||
|
||||
import java.net.URI;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
public class Send extends Activity {
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(1);
|
||||
|
||||
@Override
|
||||
public void onCreate (final Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_send);
|
||||
|
||||
final Intent intent = getIntent();
|
||||
if (intent.hasExtra (Intent.EXTRA_STREAM)) {
|
||||
final Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
||||
if (imageUri != null) {
|
||||
// handle image
|
||||
uploadImage(imageUri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void uploadImage (final Uri imageUri) {
|
||||
// handle image
|
||||
final TextView text = (TextView)findViewById(R.id.text);
|
||||
text.setText(imageUri.toString());
|
||||
|
||||
final ContentResolver resolver = getContentResolver();
|
||||
final String type = resolver.getType(imageUri);
|
||||
String extension;
|
||||
|
||||
if ("image/gif".equals(type)) {
|
||||
extension = ".gif";
|
||||
} else if ("image/png".equals(type)) {
|
||||
extension = ".png";
|
||||
} else {
|
||||
extension = ".jpg";
|
||||
}
|
||||
|
||||
final String fileName = imageUri.getLastPathSegment() + extension;
|
||||
final Server testServer = new TestServer();
|
||||
|
||||
try
|
||||
{
|
||||
final URI uploaded = executor.submit(() -> testServer.upload(fileName, new ContentByteSource(resolver, imageUri)))
|
||||
.get();
|
||||
text.setText(uploaded.toString());
|
||||
shareUploadUrl(uploaded);
|
||||
}
|
||||
catch (final InterruptedException | ExecutionException exception) {
|
||||
final StringWriter buf = new StringWriter();
|
||||
final PrintWriter out = new PrintWriter(buf);
|
||||
out.println("in: " + imageUri + " -> " + fileName);
|
||||
exception.printStackTrace(out);
|
||||
text.setText(buf.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void shareUploadUrl (final URI uploadedUrl) {
|
||||
final Intent sharingIntent = new Intent(Intent.ACTION_SEND);
|
||||
sharingIntent.setType("text/plain");
|
||||
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, uploadedUrl.toString());
|
||||
startActivity(Intent.createChooser(sharingIntent, "Share uploaded url"));
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
public static class ContentByteSource extends ByteSource {
|
||||
private final ContentResolver resolver;
|
||||
private final Uri contentUri;
|
||||
|
||||
@Override
|
||||
public InputStream openStream () throws IOException
|
||||
{
|
||||
return resolver.openInputStream(contentUri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
package net.monarchpass.piecannon;
|
||||
|
||||
import net.monarchpass.piecannon.impl.SftpServer;
|
||||
import java.net.URI;
|
||||
|
||||
public class TestServer extends SftpServer {
|
||||
public TestServer () {
|
||||
super(
|
||||
"Test Server",
|
||||
"",
|
||||
22,
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
URI.create("")
|
||||
);
|
||||
}
|
||||
}
|
@ -13,17 +13,7 @@ import com.google.common.base.Charsets;
|
||||
import com.google.common.io.CharSource;
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
public class PieCannon {
|
||||
private final List<Server> servers = new ArrayList<>();
|
||||
|
||||
public void addServer (final Server server) {
|
||||
servers.add(server);
|
||||
}
|
||||
|
||||
public List<Server> getServers () {
|
||||
return Collections.unmodifiableList(servers);
|
||||
}
|
||||
|
||||
public class PieCannon {
|
||||
public static boolean testServer (final Server server) {
|
||||
final String testString = "piecannon-test-" + System.currentTimeMillis();
|
||||
final URI result = server.upload("piecannon.test", CharSource.wrap(testString).asByteSource(Charsets.UTF_8));
|
||||
|
Loading…
x
Reference in New Issue
Block a user