Created
January 30, 2017 10:48
-
-
Save dmitriid/d1da3d09db965fb4364f595f756c28d1 to your computer and use it in GitHub Desktop.
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
package com.dmitriid; | |
import com.amazonaws.auth.AWSCredentials; | |
import com.amazonaws.auth.BasicAWSCredentials; | |
import com.amazonaws.services.s3.AmazonS3; | |
import com.amazonaws.services.s3.AmazonS3Client; | |
import com.amazonaws.services.s3.model.CannedAccessControlList; | |
import com.amazonaws.services.s3.model.ObjectMetadata; | |
import com.amazonaws.services.s3.model.PutObjectRequest; | |
import com.amazonaws.util.Base32; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | |
import com.github.davidmoten.rx.FileObservable; | |
import rx.Observable; | |
import javax.imageio.ImageIO; | |
import java.awt.image.BufferedImage; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.WatchEvent; | |
import java.util.concurrent.ExecutionException; | |
import java.util.zip.Adler32; | |
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; | |
public class QuaQua { | |
private static String awsFileName(File file) { | |
Adler32 a = new Adler32(); | |
a.update(file.getName().getBytes()); | |
return new String(Base32.encode(String.valueOf(a.getValue()).getBytes())); | |
} | |
private static byte[] toPng(File file) { | |
// read a jpeg from a inputFile | |
try { | |
BufferedImage bufferedImage = ImageIO.read(file); | |
// this writes the bufferedImage into a byte array called resultingBytes | |
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); | |
ImageIO.write(bufferedImage, "png", byteArrayOut); | |
return byteArrayOut.toByteArray(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private static void handleFile(WatchEvent watchEvent, JsonNode configuration) { | |
String path = configuration.at("/path").asText() + "/" + watchEvent.context(); | |
System.out.print("Uploading " + path + " ... "); | |
File file = new File(path); | |
System.out.print("toPng ... "); | |
byte[] result = toPng(file); | |
if (result == null) return; | |
String newFileName = QuaQua.awsFileName(file).toLowerCase() + ".png"; | |
AWSCredentials credentials = new BasicAWSCredentials(configuration.at("/accessKey").asText(), | |
configuration.at("/secretKey").asText()); | |
AmazonS3 s3client = new AmazonS3Client(credentials); | |
String bucketName = configuration.at("/bucket").asText(); | |
ObjectMetadata meta = new ObjectMetadata(); | |
meta.setContentType("image/png"); | |
meta.setContentLength(result.length); | |
PutObjectRequest req = new PutObjectRequest(bucketName, | |
configuration.at("/folder").asText() + "/" + newFileName, | |
new ByteArrayInputStream(result), | |
meta); | |
System.out.println("to S3 ... "); | |
s3client.putObject(req.withCannedAcl(CannedAccessControlList.PublicRead)); | |
System.out.println("http://dmitriid.com/i/" + newFileName); | |
} | |
private static void runMonitor(JsonNode configuration) { | |
File file = new File(configuration.at("/path").asText()); | |
Observable<WatchEvent<?>> events = FileObservable.from(file, ENTRY_CREATE); | |
events.subscribe(watchEvent -> { | |
QuaQua.handleFile(watchEvent, configuration); | |
}); | |
events.publish(); | |
} | |
static public void main(String[] args) throws ExecutionException, InterruptedException { | |
CommandLine line = new CommandLine(args); | |
String configFile = line.optionValue("config"); | |
YAMLFactory ymlFactory = new YAMLFactory(); | |
try { | |
JsonNode configuration = new ObjectMapper().readTree(ymlFactory.createParser(new File(configFile))); | |
System.out.println("Running Quaqua..."); | |
runMonitor(configuration); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment