Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update the records file aftera seed is actually received

  • Loading branch information...
commit a7c97933b3305b98299a361d55fa61e3bd9c5379 1 parent e0dacd4
mic authored
View
27 blogracy-vuze/src/main/java/net/blogracy/services/DownloadService.java
@@ -42,6 +42,7 @@
import org.gudy.azureus2.plugins.PluginInterface;
import org.gudy.azureus2.plugins.download.Download;
+import org.gudy.azureus2.plugins.download.DownloadCompletionListener;
import org.gudy.azureus2.plugins.download.DownloadException;
import org.gudy.azureus2.plugins.torrent.Torrent;
import org.gudy.azureus2.plugins.torrent.TorrentException;
@@ -61,6 +62,28 @@
* ...
*/
public class DownloadService implements MessageListener {
+ class CompletionListener implements DownloadCompletionListener {
+ private TextMessage request;
+
+ CompletionListener(TextMessage request) {
+ this.request = request;
+ }
+
+ public void onCompletion(Download d) {
+ try {
+ JSONObject record = new JSONObject(request.getText());
+ TextMessage response = session.createTextMessage();
+ response.setText(record.toString());
+ response.setJMSCorrelationID(request.getJMSCorrelationID());
+ producer.send(request.getJMSReplyTo(), response);
+ } catch (JMSException e) {
+ Logger.error("JMS error: download completion");
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
private PluginInterface plugin;
private Session session;
@@ -101,8 +124,10 @@ public void onMessage(Message request) {
.createFromBEncodedInputStream(is);
download = plugin.getDownloadManager().addDownload(torrent,
null, folder);
- if (download != null && file != null)
+ if (download != null && file != null) {
download.renameDownload(file.getName());
+ download.addCompletionListener(new CompletionListener((TextMessage) request));
+ }
Logger.info(magnetUri + " added to download list");
} catch (ResourceDownloaderException e) {
Logger.error("Torrent download error: download service: "
View
327 blogracy-web/src/main/java/net/blogracy/controller/DistributedHashTable.java
@@ -67,165 +67,170 @@
*/
public class DistributedHashTable {
- private ConnectionFactory connectionFactory;
- private Connection connection;
- private Session session;
- private Destination lookupQueue;
- private Destination storeQueue;
- private Destination downloadQueue;
- private MessageProducer producer;
- private MessageConsumer consumer;
-
- static final String CACHE_FOLDER = Configurations.getPathConfig()
- .getCachedFilesDirectoryPath();
-
- private HashMap<String, JSONObject> records = new HashMap<String, JSONObject>();
-
- private static final DistributedHashTable THE_INSTANCE = new DistributedHashTable();
-
- public static DistributedHashTable getSingleton() {
- return THE_INSTANCE;
- }
-
- public DistributedHashTable() {
- try {
- File recordsFile = new File(CACHE_FOLDER + File.separator
- + "records.json");
- if (recordsFile.exists()) {
- JSONArray recordList = new JSONArray(new JSONTokener(
- new FileReader(recordsFile)));
- for (int i = 0; i < recordList.length(); ++i) {
- JSONObject record = recordList.getJSONObject(i);
- records.put(record.getString("id"), record);
- }
- }
-
- connectionFactory = new ActiveMQConnectionFactory(
- ActiveMQConnection.DEFAULT_BROKER_URL);
- connection = connectionFactory.createConnection();
- connection.start();
- session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- producer = session.createProducer(null);
- producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- lookupQueue = session.createQueue("lookup");
- storeQueue = session.createQueue("store");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public void lookup(final String id) {
- try {
- Destination tempDest = session.createTemporaryQueue();
- MessageConsumer responseConsumer = session.createConsumer(tempDest);
- responseConsumer.setMessageListener(new MessageListener() {
- @Override
- public void onMessage(Message response) {
- try {
- String msgText = ((TextMessage) response).getText();
- JSONObject keyValue = new JSONObject(msgText);
- String value = keyValue.getString("value");
- PublicKey signerKey = JsonWebSignature
- .getSignerKey(value);
- JSONObject record = new JSONObject(JsonWebSignature
- .verify(value, signerKey));
- JSONObject currentRecord = getRecord(id);
- if (currentRecord == null
- || currentRecord.getString("version")
- .compareTo(record.getString("version")) < 0) {
- putRecord(record);
- String uri = record.getString("uri");
- FileSharing.getSingleton().download(uri, ".json");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
-
- JSONObject record = new JSONObject();
- record.put("id", id);
-
- TextMessage message = session.createTextMessage();
- message.setText(record.toString());
- message.setJMSReplyTo(tempDest);
- producer.send(lookupQueue, message);
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public void store(final String id, final String uri, final String version) {
- this.store(id, uri, version, null, null);
- }
-
- public void store(final String id, final String uri, final String version,
- final JSONArray albums, final JSONArray mediaItems) {
- try {
- JSONObject record = new JSONObject();
- record.put("id", id);
- record.put("uri", uri);
- record.put("version", version);
- // put "magic" public-key; e.g.
- // RSA.modulus(n).exponent(e)
- // record.put("signature", user); // TODO
-
- if (albums != null && albums.length() > 0) {
- record.put("albums", albums);
- }
-
- if (mediaItems != null && mediaItems.length() > 0) {
- record.put("mediaItems", mediaItems);
- }
-
- KeyPair keyPair = Configurations.getUserConfig().getUserKeyPair();
- String value = JsonWebSignature.sign(record.toString(), keyPair);
-
- JSONObject keyValue = new JSONObject();
- keyValue.put("key", id);
- keyValue.put("value", value);
- TextMessage message = session.createTextMessage();
- message.setText(keyValue.toString());
- producer.send(storeQueue, message);
- putRecord(record);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public JSONObject getRecord(String user) {
- return records.get(user);
- }
-
- public void putRecord(JSONObject record) {
- try {
- String id = record.getString("id");
- JSONObject old = records.get(id);
- if (old != null) {
- String prev = old.getString("uri");
- record.put("prev", prev);
- }
- records.put(id, record);
- } catch (JSONException e1) {
- e1.printStackTrace();
- }
- JSONArray recordList = new JSONArray();
- Iterator<JSONObject> entries = records.values().iterator();
- while (entries.hasNext()) {
- JSONObject entry = entries.next();
- recordList.put(entry);
- }
- File recordsFile = new File(CACHE_FOLDER + File.separator
- + "records.json");
- try {
- FileWriter writer = new FileWriter(recordsFile);
- recordList.write(writer);
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
+ private ConnectionFactory connectionFactory;
+ private Connection connection;
+ private Session session;
+ private Destination lookupQueue;
+ private Destination storeQueue;
+ private Destination downloadQueue;
+ private MessageProducer producer;
+ private MessageConsumer consumer;
+
+ static final String CACHE_FOLDER = Configurations.getPathConfig()
+ .getCachedFilesDirectoryPath();
+
+ private HashMap<String, JSONObject> records = new HashMap<String, JSONObject>();
+
+ private static final DistributedHashTable THE_INSTANCE = new DistributedHashTable();
+
+ public static DistributedHashTable getSingleton() {
+ return THE_INSTANCE;
+ }
+
+ public DistributedHashTable() {
+ try {
+ File recordsFile = new File(CACHE_FOLDER + File.separator
+ + "records.json");
+ if (recordsFile.exists()) {
+ JSONArray recordList = new JSONArray(new JSONTokener(
+ new FileReader(recordsFile)));
+ for (int i = 0; i < recordList.length(); ++i) {
+ JSONObject record = recordList.getJSONObject(i);
+ records.put(record.getString("id"), record);
+ }
+ }
+
+ connectionFactory = new ActiveMQConnectionFactory(
+ ActiveMQConnection.DEFAULT_BROKER_URL);
+ connection = connectionFactory.createConnection();
+ connection.start();
+ session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ producer = session.createProducer(null);
+ producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ lookupQueue = session.createQueue("lookup");
+ storeQueue = session.createQueue("store");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void lookup(final String id) {
+ try {
+ Destination tempDest = session.createTemporaryQueue();
+ MessageConsumer responseConsumer = session.createConsumer(tempDest);
+ responseConsumer.setMessageListener(new MessageListener() {
+ @Override
+ public void onMessage(Message response) {
+ try {
+ String msgText = ((TextMessage) response).getText();
+ JSONObject keyValue = new JSONObject(msgText);
+ String value = keyValue.getString("value");
+ PublicKey signerKey = JsonWebSignature
+ .getSignerKey(value);
+ final JSONObject record = new JSONObject(JsonWebSignature
+ .verify(value, signerKey));
+ JSONObject currentRecord = getRecord(id);
+ if (currentRecord == null
+ || currentRecord.getString("version")
+ .compareTo(record.getString("version")) < 0) {
+ String uri = record.getString("uri");
+ FileSharing fileSharing = FileSharing.getSingleton();
+ String hash = fileSharing.getHashFromMagnetURI(uri);
+ fileSharing.downloadByHash(hash, ".json", new MessageListener() {
+ public void onMessage(Message response) {
+ putRecord(record);
+ }
+ });
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ });
+
+ JSONObject record = new JSONObject();
+ record.put("id", id);
+
+ TextMessage message = session.createTextMessage();
+ message.setText(record.toString());
+ message.setJMSReplyTo(tempDest);
+ producer.send(lookupQueue, message);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void store(final String id, final String uri, final String version) {
+ this.store(id, uri, version, null, null);
+ }
+
+ public void store(final String id, final String uri, final String version,
+ final JSONArray albums, final JSONArray mediaItems) {
+ try {
+ JSONObject record = new JSONObject();
+ record.put("id", id);
+ record.put("uri", uri);
+ record.put("version", version);
+ // put "magic" public-key; e.g.
+ // RSA.modulus(n).exponent(e)
+ // record.put("signature", user); // TODO
+
+ if (albums != null && albums.length() > 0) {
+ record.put("albums", albums);
+ }
+
+ if (mediaItems != null && mediaItems.length() > 0) {
+ record.put("mediaItems", mediaItems);
+ }
+
+ KeyPair keyPair = Configurations.getUserConfig().getUserKeyPair();
+ String value = JsonWebSignature.sign(record.toString(), keyPair);
+
+ JSONObject keyValue = new JSONObject();
+ keyValue.put("key", id);
+ keyValue.put("value", value);
+ TextMessage message = session.createTextMessage();
+ message.setText(keyValue.toString());
+ producer.send(storeQueue, message);
+ putRecord(record);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public JSONObject getRecord(String user) {
+ return records.get(user);
+ }
+
+ public void putRecord(JSONObject record) {
+ try {
+ String id = record.getString("id");
+ JSONObject old = records.get(id);
+ if (old != null) {
+ String prev = old.getString("uri");
+ record.put("prev", prev);
+ }
+ records.put(id, record);
+ } catch (JSONException e1) {
+ e1.printStackTrace();
+ }
+ JSONArray recordList = new JSONArray();
+ Iterator<JSONObject> entries = records.values().iterator();
+ while (entries.hasNext()) {
+ JSONObject entry = entries.next();
+ recordList.put(entry);
+ }
+ File recordsFile = new File(CACHE_FOLDER + File.separator
+ + "records.json");
+ try {
+ FileWriter writer = new FileWriter(recordsFile);
+ recordList.write(writer);
+ writer.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
}
View
1,377 blogracy-web/src/main/java/net/blogracy/controller/FileSharing.java
@@ -43,7 +43,9 @@
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
+import javax.jms.Message;
import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
@@ -80,683 +82,700 @@
*/
public class FileSharing {
- private ConnectionFactory connectionFactory;
- private Connection connection;
- private Session session;
- private Destination seedQueue;
- private Destination downloadQueue;
- private MessageProducer producer;
- private MessageConsumer consumer;
-
- static final DateFormat ISO_DATE_FORMAT = new SimpleDateFormat(
- "yyyy-MM-dd'T'HH:mm:ss");
- static final String CACHE_FOLDER = Configurations.getPathConfig()
- .getCachedFilesDirectoryPath();
-
- private static final FileSharing THE_INSTANCE = new FileSharing();
-
- private static BeanJsonConverter CONVERTER = new BeanJsonConverter(
- Guice.createInjector(new Module() {
- @Override
- public void configure(Binder b) {
- b.bind(BeanConverter.class)
- .annotatedWith(
- Names.named("shindig.bean.converter.json"))
- .to(BeanJsonConverter.class);
- }
- }));
-
- public static FileSharing getSingleton() {
- return THE_INSTANCE;
- }
-
- public static String hash(String text) {
- String result = null;
- try {
- MessageDigest digester = MessageDigest.getInstance("SHA-1");
- Base32 encoder = new Base32();
- byte[] digest = digester.digest(text.getBytes());
- result = encoder.encodeAsString(digest);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- return result;
- }
-
- public static String hash(File file) {
- String result = null;
- try {
- MessageDigest digester = MessageDigest.getInstance("SHA-1");
- Base32 encoder = new Base32();
- byte[] digest = digester.digest(FileUtils.getBytesFromFile(file));
- result = encoder.encodeAsString(digest);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return result;
- }
-
- public FileSharing() {
- try {
- connectionFactory = new ActiveMQConnectionFactory(
- ActiveMQConnection.DEFAULT_BROKER_URL);
- connection = connectionFactory.createConnection();
- connection.start();
- session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- producer = session.createProducer(null);
- producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- seedQueue = session.createQueue("seed");
- downloadQueue = session.createQueue("download");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public String seed(File file) {
- String uri = null;
- try {
- Destination tempDest = session.createTemporaryQueue();
- MessageConsumer responseConsumer = session.createConsumer(tempDest);
-
- JSONObject requestObj = new JSONObject();
- requestObj.put("file", file.getAbsolutePath());
-
- TextMessage request = session.createTextMessage();
- request.setText(requestObj.toString());
- request.setJMSReplyTo(tempDest);
- producer.send(seedQueue, request);
-
- TextMessage response = (TextMessage) responseConsumer.receive();
- String msgText = ((TextMessage) response).getText();
- JSONObject responseObj = new JSONObject(msgText);
- uri = responseObj.getString("uri");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return uri;
- }
-
- static public List<ActivityEntry> getFeed(String user) {
- List<ActivityEntry> result = new ArrayList<ActivityEntry>();
- System.out.println("Getting feed: " + user);
- JSONObject record = DistributedHashTable.getSingleton().getRecord(user);
- if (record != null) {
- try {
- String latestHash = FileSharing.getHashFromMagnetURI(record
- .getString("uri"));
- File dbFile = new File(CACHE_FOLDER + File.separator
- + latestHash + ".json");
- if (! dbFile.exists() && record.has("prev")) {
- latestHash = FileSharing.getHashFromMagnetURI(record
- .getString("prev"));
- dbFile = new File(CACHE_FOLDER + File.separator
- + latestHash + ".json");
- }
- if (dbFile.exists()) {
- System.out.println("Getting feed: " + dbFile.getAbsolutePath());
- JSONObject db = new JSONObject(new JSONTokener(new FileReader(
- dbFile)));
-
- JSONArray items = db.getJSONArray("items");
- for (int i = 0; i < items.length(); ++i) {
- JSONObject item = items.getJSONObject(i);
- ActivityEntry entry = (ActivityEntry) CONVERTER
- .convertToObject(item, ActivityEntry.class);
- result.add(entry);
- }
- System.out.println("Feed loaded");
- } else {
- System.out.println("Feed not found");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return result;
- }
-
- public void addFeedEntry(String id, String text, File attachment) {
- try {
- String hash = hash(text);
- File textFile = new File(CACHE_FOLDER + File.separator + hash
- + ".txt");
-
- FileWriter w = new FileWriter(textFile);
- w.write(text);
- w.close();
-
- String textUri = seed(textFile);
- String attachmentUri = null;
- if (attachment != null) {
- attachmentUri = seed(attachment);
- }
-
- final List<ActivityEntry> feed = getFeed(id);
- final ActivityEntry entry = new ActivityEntryImpl();
- entry.setVerb("post");
- entry.setUrl(textUri);
- entry.setPublished(ISO_DATE_FORMAT.format(new Date()));
- entry.setContent(text);
- if (attachment != null) {
- ActivityObject enclosure = new ActivityObjectImpl();
- enclosure.setUrl(attachmentUri);
- entry.setObject(enclosure);
- }
- feed.add(0, entry);
- String feedUri = seedActivityStream(id, feed);
- DistributedHashTable.getSingleton().store(id, feedUri,
- entry.getPublished());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- * Create a new Album for the user.
- * Adds the album to the user's recordDB entry
- * Adds the action to the ActivityStream (verb: create)
- *
- * @param userId
- * @param photoAlbumName
- */
- public synchronized String createPhotoAlbum(String userId, String photoAlbumTitle) {
- if (userId == null)
- throw new InvalidParameterException("userId cannot be null");
-
- if (photoAlbumTitle == null || photoAlbumTitle.isEmpty())
- return null;
-
- String albumHash = null;
- try {
- albumHash = hash(userId + photoAlbumTitle);
- Album album = new AlbumImpl();
- album.setTitle(photoAlbumTitle);
- album.setId(albumHash);
- album.setOwnerId(userId);
- List<Type> types = new ArrayList<Type>();
- types.add(Type.IMAGE);
- album.setMediaType(types);
- // Album is empty where created
- album.setMediaItemCount(0);
-
- final List<ActivityEntry> feed = getFeed(userId);
- final ActivityEntry entry = new ActivityEntryImpl();
- entry.setVerb("create");
- ActivityObject mediaAlbumObject = new ActivityObjectImpl();
- mediaAlbumObject.setObjectType("collection");
- mediaAlbumObject.setContent(photoAlbumTitle);
- entry.setObject(mediaAlbumObject);
-
- entry.setPublished(ISO_DATE_FORMAT.format(new Date()));
- entry.setContent(photoAlbumTitle);
-
- feed.add(0, entry);
- String feedUri = seedActivityStream(userId, feed);
-
- // Append another album into the user's recordDB
- JSONObject recordDb = DistributedHashTable.getSingleton()
- .getRecord(userId);
-
- if (recordDb == null)
- recordDb = new JSONObject();
-
- JSONArray albums = recordDb.optJSONArray("albums");
-
- if (albums != null) {
- // Simply append new album
- albums.put(new JSONObject(CONVERTER.convertToString(album)));
- } else {
- albums = new JSONArray();
- albums.put(new JSONObject(CONVERTER.convertToString(album)));
- }
-
- DistributedHashTable.getSingleton().store(userId, feedUri,
- entry.getPublished(), albums,
- recordDb.optJSONArray("mediaItems"));
-
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return albumHash;
- }
-
- /**
- * Get the photo albums from recordDb given the userId.
- *
- * @param userId
- */
- public List<Album> getAlbums(String userId) {
- if (userId == null)
- throw new InvalidParameterException("userId cannot be null");
-
- List<Album> albums = new ArrayList<Album>();
- try {
- JSONObject recordDb = DistributedHashTable.getSingleton()
- .getRecord(userId);
-
- if (recordDb == null)
- return albums;
-
- JSONArray albumArray = recordDb.optJSONArray("albums");
-
- if (albumArray != null) {
- for (int i = 0; i < albumArray.length(); ++i) {
- JSONObject singleAlbumObject = albumArray.getJSONObject(i);
- Album entry = (Album) CONVERTER.convertToObject(
- singleAlbumObject, Album.class);
- albums.add(entry);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return albums;
- }
-
- /**
- * Get the images from recordDb given the userId and the associated albumId
- *
- * @param userId
- * @param albumId
- * @return
- */
- public List<MediaItem> getMediaItems(String userId, String albumId) {
- if (userId == null)
- throw new InvalidParameterException("userId cannot be null");
-
- if (albumId == null)
- throw new InvalidParameterException("albumId cannot be null");
-
- List<MediaItem> mediaItems = new ArrayList<MediaItem>();
-
- try {
- JSONObject recordDb = DistributedHashTable.getSingleton()
- .getRecord(userId);
-
- if (recordDb == null)
- return mediaItems;
-
- JSONArray mediaItemsArray = recordDb.optJSONArray("mediaItems");
-
- if (mediaItemsArray != null) {
- for (int i = 0; i < mediaItemsArray.length(); ++i) {
- JSONObject singleAlbumObject = mediaItemsArray
- .getJSONObject(i);
- MediaItem entry = (MediaItem) CONVERTER.convertToObject(
- singleAlbumObject, MediaItem.class);
-
- if (entry.getAlbumId().equals(albumId))
- mediaItems.add(entry);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return mediaItems;
- }
-
- /**
- * Gets the images from recordDb given the userId and the associated albumId.
- * Attempts to download the images from DHT. If successful, set's the URL
- * with the cached image link
- *
- * @param userId
- * @param albumId
- * @return
- */
- public List<MediaItem> getMediaItemsWithCachedImages(String userId,
- String albumId) {
-
- if (userId == null)
- throw new InvalidParameterException("userId cannot be null");
-
- if (albumId == null)
- throw new InvalidParameterException("albumId cannot be null");
-
- List<MediaItem> mediaItems = this.getMediaItems(userId, albumId);
-
- for (MediaItem item : mediaItems) {
- String itemMagneUri = item.getUrl();
- download(itemMagneUri);
- item.setUrl("cache/" + getHashFromMagnetURI(itemMagneUri));
- }
-
- return mediaItems;
- }
-
- /***
- * Add multiple MediaItems to an album.
- * It updates the user's recordDb and notifies the action in the user's Activity Stream (verb: add)
- * @param userId
- * @param albumId
- * @param photos
- * @return
- */
- public synchronized List<String> addMediaItemsToAlbum(String userId, String albumId,
- Map<File, String> photos) {
- if (photos == null)
- return null;
-
- if (userId == null)
- throw new InvalidParameterException("userId cannot be null");
-
- if (albumId == null)
- throw new InvalidParameterException("albumId cannot be null");
-
- Album album = null;
- for (Album a : this.getAlbums(userId)) {
- if (a.getId().equals(albumId)) {
- album = a;
- break;
- }
- }
-
- if (album == null)
- throw new InvalidParameterException("AlbumId " + albumId
- + " does not match to a valid album for the user " + userId);
-
- List<String> hashList = new ArrayList<String>();
- List<MediaItem> listOfMediaItems = new ArrayList<MediaItem>();
-
- final List<ActivityEntry> feed = getFeed(userId);
- final String publishedDate = ISO_DATE_FORMAT.format(new Date());
-
- try {
-
- for (Entry<File, String> mapEntry : photos.entrySet()) {
- File photo = mapEntry.getKey();
- String mimeType = mapEntry.getValue();
- String fileHash = hash(photo);
-
- final File photoCachedFile = new File(CACHE_FOLDER
- + File.separator + fileHash);
-
- FileUtils.copyFile(photo, photoCachedFile);
- photo.delete();
-
- final String fileUrl = this.seed(photoCachedFile);
-
- final ActivityEntry entry = new ActivityEntryImpl();
- entry.setVerb("add");
- entry.setPublished(publishedDate);
- entry.setContent(getHashFromMagnetURI(fileUrl));
-
- ActivityObject mediaItemObject = new ActivityObjectImpl();
- mediaItemObject.setObjectType("image");
- mediaItemObject.setContent(getHashFromMagnetURI(fileUrl));
- mediaItemObject.setUrl(fileUrl);
- entry.setObject(mediaItemObject);
-
- ActivityObject mediaAlbumObject = new ActivityObjectImpl();
- mediaAlbumObject.setObjectType("collection");
- mediaAlbumObject.setContent(album.getTitle());
- mediaAlbumObject.setId(album.getId());
- entry.setTarget(mediaAlbumObject);
-
- feed.add(0, entry);
-
- MediaItem mediaItem = new MediaItemImpl();
- mediaItem.setAlbumId(albumId);
- mediaItem.setId(getHashFromMagnetURI(fileUrl));
- mediaItem.setUrl(fileUrl);
- mediaItem.setLastUpdated(publishedDate);
- mediaItem.setMimeType(mimeType);
-
- if (album.getMediaMimeType() == null)
- album.setMediaMimeType(new ArrayList<String>());
-
- List<String> albumMimeTypes = album.getMediaMimeType();
-
- if(!albumMimeTypes.contains(mimeType))
- albumMimeTypes.add(mimeType);
-
- listOfMediaItems.add(mediaItem);
- hashList.add(getHashFromMagnetURI(fileUrl));
- }
-
- album.setMediaItemCount(album.getMediaItemCount() + photos.size());
-
- String feedUri = seedActivityStream(userId, feed);
-
- // Update the album accordingly
- JSONObject recordDb = DistributedHashTable.getSingleton()
- .getRecord(userId);
-
- if (recordDb == null)
- recordDb = new JSONObject();
-
- JSONArray albums = recordDb.optJSONArray("albums");
-
- for (int i = 0; i < albums.length(); ++i) {
- JSONObject singleAlbumObject = albums.getJSONObject(i);
- Album entry1 = (Album) CONVERTER.convertToObject(
- singleAlbumObject, Album.class);
-
- if (entry1.getId().equals(albumId)) {
- albums.put(i,
- new JSONObject(CONVERTER.convertToString(album)));
- break;
- }
- }
-
- // Add all the newly created mediaItems
- JSONArray mediaItems = recordDb.optJSONArray("mediaItems");
-
- if (mediaItems == null)
- mediaItems = new JSONArray();
-
- for (MediaItem mediaItem : listOfMediaItems) {
- // Simply append new album
- mediaItems.put(new JSONObject(CONVERTER
- .convertToString(mediaItem)));
- }
-
- DistributedHashTable.getSingleton().store(userId, feedUri,
- publishedDate, albums, mediaItems);
-
- return hashList;
-
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
- /***
- * Adds a single MediaItem file to an Album.
- * It updates the user's recordDb and notifies the action in the user's Activity Stream (verb: remove)
- * @param userId
- * @param albumId
- * @param photo
- * @param mimeType
- * @return
- */
- public synchronized String addMediaItemToAlbum(String userId, String albumId,
- File photo, String mimeType) {
- Map<File, String> map = new HashMap<File, String>();
- map.put(photo, mimeType);
- List<String> hashes = this.addMediaItemsToAlbum(userId, albumId, map);
-
- return (hashes != null && !hashes.isEmpty()) ? hashes.get(0) : null;
- }
-
- /***
- * A Media Item is removed from an album
- * @param userId
- * @param albumId
- * @param mediaId
- */
- public synchronized void deletePhotoFromAlbum(String userId, String albumId,
- String mediaId) {
- if (mediaId == null)
- throw new InvalidParameterException("mediaId cannot be null");
-
- if (userId == null)
- throw new InvalidParameterException("userId cannot be null");
-
- if (albumId == null)
- throw new InvalidParameterException("albumId cannot be null");
-
- Album album = null;
- for (Album a : this.getAlbums(userId)) {
- if (a.getId().equals(albumId)) {
- album = a;
- break;
- }
- }
-
- if (album == null)
- throw new InvalidParameterException("AlbumId " + albumId
- + " does not correspond to a valid album for the user "
- + userId);
- try {
-
- List<MediaItem> mediaItems = this.getMediaItems(userId, albumId);
-
- for (Iterator<MediaItem> iter = mediaItems.iterator(); iter
- .hasNext();) {
- MediaItem mediaItem = iter.next();
- if (mediaId.equals(mediaItem.getId())
- && albumId.equals(mediaItem.getAlbumId()))
- iter.remove();
- }
-
- album.setMediaItemCount(mediaItems.size());
-
- final List<ActivityEntry> feed = getFeed(userId);
- final ActivityEntry entry = new ActivityEntryImpl();
- entry.setVerb("remove");
- entry.setPublished(ISO_DATE_FORMAT.format(new Date()));
- entry.setContent(mediaId);
-
- ActivityObject mediaItemObject = new ActivityObjectImpl();
- mediaItemObject.setObjectType("image");
- mediaItemObject.setContent(mediaId);
- entry.setObject(mediaItemObject);
-
- ActivityObject mediaAlbumObject = new ActivityObjectImpl();
- mediaAlbumObject.setObjectType("collection");
- mediaAlbumObject.setContent(album.getTitle());
- mediaAlbumObject.setId(album.getId());
- entry.setTarget(mediaAlbumObject);
-
- feed.add(0, entry);
- String feedUri = seedActivityStream(userId, feed);
-
- JSONObject recordDb = DistributedHashTable.getSingleton()
- .getRecord(userId);
-
- if (recordDb == null)
- recordDb = new JSONObject();
-
- JSONArray albums = recordDb.optJSONArray("albums");
-
- // update albums
- if (albums != null) {
- for (int i = 0; i < albums.length(); ++i) {
- JSONObject singleAlbumObject = albums.getJSONObject(i);
- Album entry1 = (Album) CONVERTER.convertToObject(
- singleAlbumObject, Album.class);
-
- if (entry1.getId().equals(albumId)) {
- albums.put(
- i,
- new JSONObject(CONVERTER.convertToString(album)));
- break;
- }
- }
- }
-
- JSONArray list = new JSONArray();
- JSONArray mediaItemsArray = recordDb.optJSONArray("mediaItems");
- if (mediaItemsArray != null) {
- for (int i = 0; i < mediaItemsArray.length(); ++i) {
- JSONObject singleMediaItemObject = mediaItemsArray
- .getJSONObject(i);
- MediaItem entry1 = (MediaItem) CONVERTER.convertToObject(
- singleMediaItemObject, MediaItem.class);
- if (!mediaId.equals(entry1.getId())
- || !albumId.equals(entry1.getAlbumId()))
- list.put(singleMediaItemObject);
- }
- }
-
- DistributedHashTable.getSingleton().store(userId, feedUri,
- entry.getPublished(), albums, list);
-
- } catch (Exception e) {
- e.printStackTrace();
-
- }
- }
-
- private String seedActivityStream(String userId,
- final List<ActivityEntry> feed) throws JSONException, IOException {
- final File feedFile = new File(CACHE_FOLDER + File.separator + userId
- + ".json");
-
- JSONArray items = new JSONArray();
- for (int i = 0; i < feed.size(); ++i) {
- JSONObject item = new JSONObject(feed.get(i));
- items.put(item);
- }
- JSONObject db = new JSONObject();
-
- db.put("items", items);
-
- FileWriter writer = new FileWriter(feedFile);
- db.write(writer);
- writer.close();
-
- String feedUri = seed(feedFile);
- return feedUri;
- }
-
- static String getHashFromMagnetURI(String uri) {
- String hash = null;
- int btih = uri.indexOf("xt=urn:btih:");
- if (btih >= 0) {
- hash = uri.substring(btih + "xt=urn:btih:".length());
- int amp = hash.indexOf('&');
- if (amp >= 0)
- hash = hash.substring(0, amp);
- }
- return hash;
- }
-
- public void download(final String uri) {
- String hash = getHashFromMagnetURI(uri);
- downloadByHash(hash, null);
- }
-
- public void download(final String uri, final String ext) {
- String hash = getHashFromMagnetURI(uri);
- downloadByHash(hash, ext);
- }
-
- public void downloadByHash(final String hash) {
- downloadByHash(hash, null);
- }
-
- public void downloadByHash(final String hash, final String ext) {
- try {
- String file = CACHE_FOLDER + File.separator + hash;
- if (ext != null) file += ext;
- JSONObject sharedFile = new JSONObject();
- sharedFile.put("uri", "magnet:?xt=urn:btih:" + hash);
- sharedFile.put("file", file);
-
- TextMessage message = session.createTextMessage();
- message.setText(sharedFile.toString());
- producer.send(downloadQueue, message);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
+ private ConnectionFactory connectionFactory;
+ private Connection connection;
+ private Session session;
+ private Session seedSession;
+ private Destination seedQueue;
+ private Destination downloadQueue;
+ private MessageProducer producer;
+ private MessageProducer seedProducer;
+ private MessageConsumer consumer;
+
+ static final DateFormat ISO_DATE_FORMAT = new SimpleDateFormat(
+ "yyyy-MM-dd'T'HH:mm:ss");
+ static final String CACHE_FOLDER = Configurations.getPathConfig()
+ .getCachedFilesDirectoryPath();
+
+ private static final FileSharing THE_INSTANCE = new FileSharing();
+
+ private static BeanJsonConverter CONVERTER = new BeanJsonConverter(
+ Guice.createInjector(new Module() {
+ @Override
+ public void configure(Binder b) {
+ b.bind(BeanConverter.class)
+ .annotatedWith(
+ Names.named("shindig.bean.converter.json"))
+ .to(BeanJsonConverter.class);
+ }
+ }));
+
+ public static FileSharing getSingleton() {
+ return THE_INSTANCE;
+ }
+
+ public static String hash(String text) {
+ String result = null;
+ try {
+ MessageDigest digester = MessageDigest.getInstance("SHA-1");
+ Base32 encoder = new Base32();
+ byte[] digest = digester.digest(text.getBytes());
+ result = encoder.encodeAsString(digest);
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ }
+ return result;
+ }
+
+ public static String hash(File file) {
+ String result = null;
+ try {
+ MessageDigest digester = MessageDigest.getInstance("SHA-1");
+ Base32 encoder = new Base32();
+ byte[] digest = digester.digest(FileUtils.getBytesFromFile(file));
+ result = encoder.encodeAsString(digest);
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return result;
+ }
+
+ public FileSharing() {
+ try {
+ connectionFactory = new ActiveMQConnectionFactory(
+ ActiveMQConnection.DEFAULT_BROKER_URL);
+ connection = connectionFactory.createConnection();
+ connection.start();
+ session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ producer = session.createProducer(null);
+ producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ seedSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ seedProducer = seedSession.createProducer(null);
+ seedProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+ seedQueue = session.createQueue("seed");
+ downloadQueue = session.createQueue("download");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public String seed(File file) {
+ String uri = null;
+ try {
+ Destination tempDest = seedSession.createTemporaryQueue();
+ MessageConsumer responseConsumer = seedSession.createConsumer(tempDest);
+
+ JSONObject requestObj = new JSONObject();
+ requestObj.put("file", file.getAbsolutePath());
+
+ TextMessage request = session.createTextMessage();
+ request.setText(requestObj.toString());
+ request.setJMSReplyTo(tempDest);
+ seedProducer.send(seedQueue, request);
+
+ TextMessage response = (TextMessage) responseConsumer.receive();
+ String msgText = ((TextMessage) response).getText();
+ JSONObject responseObj = new JSONObject(msgText);
+ uri = responseObj.getString("uri");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return uri;
+ }
+
+ static public List<ActivityEntry> getFeed(String user) {
+ List<ActivityEntry> result = new ArrayList<ActivityEntry>();
+ System.out.println("Getting feed: " + user);
+ JSONObject record = DistributedHashTable.getSingleton().getRecord(user);
+ if (record != null) {
+ try {
+ String latestHash = FileSharing.getHashFromMagnetURI(record
+ .getString("uri"));
+ File dbFile = new File(CACHE_FOLDER + File.separator
+ + latestHash + ".json");
+ if (! dbFile.exists() && record.has("prev")) {
+ latestHash = FileSharing.getHashFromMagnetURI(record
+ .getString("prev"));
+ dbFile = new File(CACHE_FOLDER + File.separator
+ + latestHash + ".json");
+ }
+ if (dbFile.exists()) {
+ System.out.println("Getting feed: " + dbFile.getAbsolutePath());
+ JSONObject db = new JSONObject(new JSONTokener(new FileReader(
+ dbFile)));
+
+ JSONArray items = db.getJSONArray("items");
+ for (int i = 0; i < items.length(); ++i) {
+ JSONObject item = items.getJSONObject(i);
+ ActivityEntry entry = (ActivityEntry) CONVERTER
+ .convertToObject(item, ActivityEntry.class);
+ result.add(entry);
+ }
+ System.out.println("Feed loaded");
+ } else {
+ System.out.println("Feed not found");
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ return result;
+ }
+
+ public void addFeedEntry(String id, String text, File attachment) {
+ try {
+ String hash = hash(text);
+ File textFile = new File(CACHE_FOLDER + File.separator + hash
+ + ".txt");
+
+ FileWriter w = new FileWriter(textFile);
+ w.write(text);
+ w.close();
+
+ String textUri = seed(textFile);
+ String attachmentUri = null;
+ if (attachment != null) {
+ attachmentUri = seed(attachment);
+ }
+
+ final List<ActivityEntry> feed = getFeed(id);
+ final ActivityEntry entry = new ActivityEntryImpl();
+ entry.setVerb("post");
+ entry.setUrl(textUri);
+ entry.setPublished(ISO_DATE_FORMAT.format(new Date()));
+ entry.setContent(text);
+ if (attachment != null) {
+ ActivityObject enclosure = new ActivityObjectImpl();
+ enclosure.setUrl(attachmentUri);
+ entry.setObject(enclosure);
+ }
+ feed.add(0, entry);
+ String feedUri = seedActivityStream(id, feed);
+ DistributedHashTable.getSingleton().store(id, feedUri,
+ entry.getPublished());
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Create a new Album for the user.
+ * Adds the album to the user's recordDB entry
+ * Adds the action to the ActivityStream (verb: create)
+ *
+ * @param userId
+ * @param photoAlbumName
+ */
+ public synchronized String createPhotoAlbum(String userId, String photoAlbumTitle) {
+ if (userId == null)
+ throw new InvalidParameterException("userId cannot be null");
+
+ if (photoAlbumTitle == null || photoAlbumTitle.isEmpty())
+ return null;
+
+ String albumHash = null;
+ try {
+ albumHash = hash(userId + photoAlbumTitle);
+ Album album = new AlbumImpl();
+ album.setTitle(photoAlbumTitle);
+ album.setId(albumHash);
+ album.setOwnerId(userId);
+ List<Type> types = new ArrayList<Type>();
+ types.add(Type.IMAGE);
+ album.setMediaType(types);
+ // Album is empty where created
+ album.setMediaItemCount(0);
+
+ final List<ActivityEntry> feed = getFeed(userId);
+ final ActivityEntry entry = new ActivityEntryImpl();
+ entry.setVerb("create");
+ ActivityObject mediaAlbumObject = new ActivityObjectImpl();
+ mediaAlbumObject.setObjectType("collection");
+ mediaAlbumObject.setContent(photoAlbumTitle);
+ entry.setObject(mediaAlbumObject);
+
+ entry.setPublished(ISO_DATE_FORMAT.format(new Date()));
+ entry.setContent(photoAlbumTitle);
+
+ feed.add(0, entry);
+ String feedUri = seedActivityStream(userId, feed);
+
+ // Append another album into the user's recordDB
+ JSONObject recordDb = DistributedHashTable.getSingleton()
+ .getRecord(userId);
+
+ if (recordDb == null)
+ recordDb = new JSONObject();
+
+ JSONArray albums = recordDb.optJSONArray("albums");
+
+ if (albums != null) {
+ // Simply append new album
+ albums.put(new JSONObject(CONVERTER.convertToString(album)));
+ } else {
+ albums = new JSONArray();
+ albums.put(new JSONObject(CONVERTER.convertToString(album)));
+ }
+
+ DistributedHashTable.getSingleton().store(userId, feedUri,
+ entry.getPublished(), albums,
+ recordDb.optJSONArray("mediaItems"));
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return albumHash;
+ }
+
+ /**
+ * Get the photo albums from recordDb given the userId.
+ *
+ * @param userId
+ */
+ public List<Album> getAlbums(String userId) {
+ if (userId == null)
+ throw new InvalidParameterException("userId cannot be null");
+
+ List<Album> albums = new ArrayList<Album>();
+ try {
+ JSONObject recordDb = DistributedHashTable.getSingleton()
+ .getRecord(userId);
+
+ if (recordDb == null)
+ return albums;
+
+ JSONArray albumArray = recordDb.optJSONArray("albums");
+
+ if (albumArray != null) {
+ for (int i = 0; i < albumArray.length(); ++i) {
+ JSONObject singleAlbumObject = albumArray.getJSONObject(i);
+ Album entry = (Album) CONVERTER.convertToObject(
+ singleAlbumObject, Album.class);
+ albums.add(entry);
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return albums;
+ }
+
+ /**
+ * Get the images from recordDb given the userId and the associated albumId
+ *
+ * @param userId
+ * @param albumId
+ * @return
+ */
+ public List<MediaItem> getMediaItems(String userId, String albumId) {
+ if (userId == null)
+ throw new InvalidParameterException("userId cannot be null");
+
+ if (albumId == null)
+ throw new InvalidParameterException("albumId cannot be null");
+
+ List<MediaItem> mediaItems = new ArrayList<MediaItem>();
+
+ try {
+ JSONObject recordDb = DistributedHashTable.getSingleton()
+ .getRecord(userId);
+
+ if (recordDb == null)
+ return mediaItems;
+
+ JSONArray mediaItemsArray = recordDb.optJSONArray("mediaItems");
+
+ if (mediaItemsArray != null) {
+ for (int i = 0; i < mediaItemsArray.length(); ++i) {
+ JSONObject singleAlbumObject = mediaItemsArray
+ .getJSONObject(i);
+ MediaItem entry = (MediaItem) CONVERTER.convertToObject(
+ singleAlbumObject, MediaItem.class);
+
+ if (entry.getAlbumId().equals(albumId))
+ mediaItems.add(entry);
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return mediaItems;
+ }
+
+ /**
+ * Gets the images from recordDb given the userId and the associated albumId.
+ * Attempts to download the images from DHT. If successful, set's the URL
+ * with the cached image link
+ *
+ * @param userId
+ * @param albumId
+ * @return
+ */
+ public List<MediaItem> getMediaItemsWithCachedImages(String userId,
+ String albumId) {
+
+ if (userId == null)
+ throw new InvalidParameterException("userId cannot be null");
+
+ if (albumId == null)
+ throw new InvalidParameterException("albumId cannot be null");
+
+ List<MediaItem> mediaItems = this.getMediaItems(userId, albumId);
+
+ for (MediaItem item : mediaItems) {
+ String itemMagneUri = item.getUrl();
+ download(itemMagneUri);
+ item.setUrl("cache/" + getHashFromMagnetURI(itemMagneUri));
+ }
+
+ return mediaItems;
+ }
+
+ /***
+ * Add multiple MediaItems to an album.
+ * It updates the user's recordDb and notifies the action in the user's Activity Stream (verb: add)
+ * @param userId
+ * @param albumId
+ * @param photos
+ * @return
+ */
+ public synchronized List<String> addMediaItemsToAlbum(String userId, String albumId,
+ Map<File, String> photos) {
+ if (photos == null)
+ return null;
+
+ if (userId == null)
+ throw new InvalidParameterException("userId cannot be null");
+
+ if (albumId == null)
+ throw new InvalidParameterException("albumId cannot be null");
+
+ Album album = null;
+ for (Album a : this.getAlbums(userId)) {
+ if (a.getId().equals(albumId)) {
+ album = a;
+ break;
+ }
+ }
+
+ if (album == null)
+ throw new InvalidParameterException("AlbumId " + albumId
+ + " does not match to a valid album for the user " + userId);
+
+ List<String> hashList = new ArrayList<String>();
+ List<MediaItem> listOfMediaItems = new ArrayList<MediaItem>();
+
+ final List<ActivityEntry> feed = getFeed(userId);
+ final String publishedDate = ISO_DATE_FORMAT.format(new Date());
+
+ try {
+
+ for (Entry<File, String> mapEntry : photos.entrySet()) {
+ File photo = mapEntry.getKey();
+ String mimeType = mapEntry.getValue();
+ String fileHash = hash(photo);
+
+ final File photoCachedFile = new File(CACHE_FOLDER
+ + File.separator + fileHash);
+
+ FileUtils.copyFile(photo, photoCachedFile);
+ photo.delete();
+
+ final String fileUrl = this.seed(photoCachedFile);
+
+ final ActivityEntry entry = new ActivityEntryImpl();
+ entry.setVerb("add");
+ entry.setPublished(publishedDate);
+ entry.setContent(getHashFromMagnetURI(fileUrl));
+
+ ActivityObject mediaItemObject = new ActivityObjectImpl();
+ mediaItemObject.setObjectType("image");
+ mediaItemObject.setContent(getHashFromMagnetURI(fileUrl));
+ mediaItemObject.setUrl(fileUrl);
+ entry.setObject(mediaItemObject);
+
+ ActivityObject mediaAlbumObject = new ActivityObjectImpl();
+ mediaAlbumObject.setObjectType("collection");
+ mediaAlbumObject.setContent(album.getTitle());
+ mediaAlbumObject.setId(album.getId());
+ entry.setTarget(mediaAlbumObject);
+
+ feed.add(0, entry);
+
+ MediaItem mediaItem = new MediaItemImpl();
+ mediaItem.setAlbumId(albumId);
+ mediaItem.setId(getHashFromMagnetURI(fileUrl));
+ mediaItem.setUrl(fileUrl);
+ mediaItem.setLastUpdated(publishedDate);
+ mediaItem.setMimeType(mimeType);
+
+ if (album.getMediaMimeType() == null)
+ album.setMediaMimeType(new ArrayList<String>());
+
+ List<String> albumMimeTypes = album.getMediaMimeType();
+
+ if(!albumMimeTypes.contains(mimeType))
+ albumMimeTypes.add(mimeType);
+
+ listOfMediaItems.add(mediaItem);
+ hashList.add(getHashFromMagnetURI(fileUrl));
+ }
+
+ album.setMediaItemCount(album.getMediaItemCount() + photos.size());
+
+ String feedUri = seedActivityStream(userId, feed);
+
+ // Update the album accordingly
+ JSONObject recordDb = DistributedHashTable.getSingleton()
+ .getRecord(userId);
+
+ if (recordDb == null)
+ recordDb = new JSONObject();
+
+ JSONArray albums = recordDb.optJSONArray("albums");
+
+ for (int i = 0; i < albums.length(); ++i) {
+ JSONObject singleAlbumObject = albums.getJSONObject(i);
+ Album entry1 = (Album) CONVERTER.convertToObject(
+ singleAlbumObject, Album.class);
+
+ if (entry1.getId().equals(albumId)) {
+ albums.put(i,
+ new JSONObject(CONVERTER.convertToString(album)));
+ break;
+ }
+ }
+
+ // Add all the newly created mediaItems
+ JSONArray mediaItems = recordDb.optJSONArray("mediaItems");
+
+ if (mediaItems == null)
+ mediaItems = new JSONArray();
+
+ for (MediaItem mediaItem : listOfMediaItems) {
+ // Simply append new album
+ mediaItems.put(new JSONObject(CONVERTER
+ .convertToString(mediaItem)));
+ }
+
+ DistributedHashTable.getSingleton().store(userId, feedUri,
+ publishedDate, albums, mediaItems);
+
+ return hashList;
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ /***
+ * Adds a single MediaItem file to an Album.
+ * It updates the user's recordDb and notifies the action
+ * in the user's Activity Stream (verb: remove)
+ * @param userId
+ * @param albumId
+ * @param photo
+ * @param mimeType
+ * @return
+ */
+ public synchronized String addMediaItemToAlbum(String userId, String albumId,
+ File photo, String mimeType) {
+ Map<File, String> map = new HashMap<File, String>();
+ map.put(photo, mimeType);
+ List<String> hashes = this.addMediaItemsToAlbum(userId, albumId, map);
+
+ return (hashes != null && !hashes.isEmpty()) ? hashes.get(0) : null;
+ }
+
+ /***
+ * A Media Item is removed from an album
+ * @param userId
+ * @param albumId
+ * @param mediaId
+ */
+ public synchronized void deletePhotoFromAlbum(String userId, String albumId,
+ String mediaId) {
+ if (mediaId == null)
+ throw new InvalidParameterException("mediaId cannot be null");
+
+ if (userId == null)
+ throw new InvalidParameterException("userId cannot be null");
+
+ if (albumId == null)
+ throw new InvalidParameterException("albumId cannot be null");
+
+ Album album = null;
+ for (Album a : this.getAlbums(userId)) {
+ if (a.getId().equals(albumId)) {
+ album = a;
+ break;
+ }
+ }
+
+ if (album == null)
+ throw new InvalidParameterException("AlbumId " + albumId
+ + " does not correspond to a valid album for the user "
+ + userId);
+ try {
+
+ List<MediaItem> mediaItems = this.getMediaItems(userId, albumId);
+
+ for (Iterator<MediaItem> iter = mediaItems.iterator(); iter
+ .hasNext();) {
+ MediaItem mediaItem = iter.next();
+ if (mediaId.equals(mediaItem.getId())
+ && albumId.equals(mediaItem.getAlbumId()))
+ iter.remove();
+ }
+
+ album.setMediaItemCount(mediaItems.size());
+
+ final List<ActivityEntry> feed = getFeed(userId);
+ final ActivityEntry entry = new ActivityEntryImpl();
+ entry.setVerb("remove");
+ entry.setPublished(ISO_DATE_FORMAT.format(new Date()));
+ entry.setContent(mediaId);
+
+ ActivityObject mediaItemObject = new ActivityObjectImpl();
+ mediaItemObject.setObjectType("image");
+ mediaItemObject.setContent(mediaId);
+ entry.setObject(mediaItemObject);
+
+ ActivityObject mediaAlbumObject = new ActivityObjectImpl();
+ mediaAlbumObject.setObjectType("collection");
+ mediaAlbumObject.setContent(album.getTitle());
+ mediaAlbumObject.setId(album.getId());
+ entry.setTarget(mediaAlbumObject);
+
+ feed.add(0, entry);
+ String feedUri = seedActivityStream(userId, feed);
+
+ JSONObject recordDb = DistributedHashTable.getSingleton()
+ .getRecord(userId);
+
+ if (recordDb == null)
+ recordDb = new JSONObject();
+
+ JSONArray albums = recordDb.optJSONArray("albums");
+
+ // update albums
+ if (albums != null) {
+ for (int i = 0; i < albums.length(); ++i) {
+ JSONObject singleAlbumObject = albums.getJSONObject(i);
+ Album entry1 = (Album) CONVERTER.convertToObject(
+ singleAlbumObject, Album.class);
+
+ if (entry1.getId().equals(albumId)) {
+ albums.put(
+ i,
+ new JSONObject(CONVERTER.convertToString(album)));
+ break;
+ }
+ }
+ }
+
+ JSONArray list = new JSONArray();
+ JSONArray mediaItemsArray = recordDb.optJSONArray("mediaItems");
+ if (mediaItemsArray != null) {
+ for (int i = 0; i < mediaItemsArray.length(); ++i) {
+ JSONObject singleMediaItemObject = mediaItemsArray
+ .getJSONObject(i);
+ MediaItem entry1 = (MediaItem) CONVERTER.convertToObject(
+ singleMediaItemObject, MediaItem.class);
+ if (!mediaId.equals(entry1.getId())
+ || !albumId.equals(entry1.getAlbumId()))
+ list.put(singleMediaItemObject);
+ }
+ }
+
+ DistributedHashTable.getSingleton().store(userId, feedUri,
+ entry.getPublished(), albums, list);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+
+ }
+ }
+
+ private String seedActivityStream(String userId,
+ final List<ActivityEntry> feed) throws JSONException, IOException {
+ final File feedFile = new File(CACHE_FOLDER + File.separator + userId
+ + ".json");
+
+ JSONArray items = new JSONArray();
+ for (int i = 0; i < feed.size(); ++i) {
+ JSONObject item = new JSONObject(feed.get(i));
+ items.put(item);
+ }
+ JSONObject db = new JSONObject();
+
+ db.put("items", items);
+
+ FileWriter writer = new FileWriter(feedFile);
+ db.write(writer);
+ writer.close();
+
+ String feedUri = seed(feedFile);
+ return feedUri;
+ }
+
+ static String getHashFromMagnetURI(String uri) {
+ String hash = null;
+ int btih = uri.indexOf("xt=urn:btih:");
+ if (btih >= 0) {
+ hash = uri.substring(btih + "xt=urn:btih:".length());
+ int amp = hash.indexOf('&');
+ if (amp >= 0)
+ hash = hash.substring(0, amp);
+ }
+ return hash;
+ }
+
+ public void download(final String uri) {
+ String hash = getHashFromMagnetURI(uri);
+ downloadByHash(hash, null, null);
+ }
+
+ public void download(final String uri, final String ext) {
+ String hash = getHashFromMagnetURI(uri);
+ downloadByHash(hash, ext, null);
+ }
+
+ public void downloadByHash(final String hash) {
+ downloadByHash(hash, null, null);
+ }
+
+ public void downloadByHash(final String hash, final String ext, final MessageListener listener) {
+ try {
+ Destination tempDest = session.createTemporaryQueue();
+ MessageConsumer responseConsumer = session.createConsumer(tempDest);
+ if (listener != null) {
+ responseConsumer.setMessageListener(listener);
+ } else {
+ responseConsumer.setMessageListener(new MessageListener() {
+ @Override
+ public void onMessage(Message response) {}
+ });
+ }
+
+ String file = CACHE_FOLDER + File.separator + hash;
+ if (ext != null) file += ext;
+ JSONObject sharedFile = new JSONObject();
+ sharedFile.put("uri", "magnet:?xt=urn:btih:" + hash);
+ sharedFile.put("file", file);
+
+ TextMessage message = session.createTextMessage();
+ message.setText(sharedFile.toString());
+ message.setJMSReplyTo(tempDest);
+ producer.send(downloadQueue, message);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.