diff --git a/core/src/main/groovy/com/muwire/core/filefeeds/FeedItems.groovy b/core/src/main/groovy/com/muwire/core/filefeeds/FeedItems.groovy index c2519aa6..41b16959 100644 --- a/core/src/main/groovy/com/muwire/core/filefeeds/FeedItems.groovy +++ b/core/src/main/groovy/com/muwire/core/filefeeds/FeedItems.groovy @@ -1,6 +1,9 @@ package com.muwire.core.filefeeds +import com.muwire.core.InfoHash +import com.muwire.core.Persona import com.muwire.core.SharedFile +import com.muwire.core.files.FileHasher import com.muwire.core.util.DataUtil import net.i2p.data.Base64 @@ -25,4 +28,52 @@ class FeedItems { json } + + public static FeedItem objToFeedItem(def obj, Persona publisher) throws InvalidFeedItemException { + if (obj.timestamp == null) + throw new InvalidFeedItemException("No timestamp"); + if (obj.name == null) + throw new InvalidFeedItemException("No name"); + if (obj.size == null || obj.size <= 0 || obj.size > FileHasher.MAX_SIZE) + throw new InvalidFeedItemException("length missing or invalid ${obj.size}") + if (obj.pieceSize == null || obj.pieceSize < FileHasher.MIN_PIECE_SIZE_POW2 || obj.pieceSize > FileHasher.MAX_PIECE_SIZE_POW2) + throw new InvalidFeedItemException("piece size missing or invalid ${obj.pieceSize}") + if (obj.infoHash == null) + throw new InvalidFeedItemException("Infohash missing") + + + InfoHash infoHash + try { + infoHash = new InfoHash(Base64.decode(obj.infoHash)) + } catch (Exception bad) { + throw new InvalidFeedItemException("Invalid infohash", bad) + } + + String name + try { + name = DataUtil.readi18nString(Base64.decode(obj.name)) + } catch (Exception bad) { + throw new InvalidFeedItemException("Invalid name", bad) + } + + int certificates = 0 + if (obj.certificates != null) + certificates = obj.certificates + + new FeedItem(publisher, obj.timestamp, name, obj.size, obj.pieceSize, infoHash, certificates, obj.comment) + } + + public static def feedItemToObj(FeedItem item) { + def json = [:] + json.type = "FeedItem" + json.version = 1 + json.name = Base64.encode(DataUtil.encodei18nString(item.getName())) + json.infoHash = Base64.encode(item.getInfoHash().getRoot()) + json.size = item.getSize() + json.pieceSize = item.getPieceSize() + json.timestamp = item.getTimestamp() + json.certificates = item.getCertificates() + json.comment = item.getComment() + json + } } diff --git a/core/src/main/java/com/muwire/core/filefeeds/FeedItem.java b/core/src/main/java/com/muwire/core/filefeeds/FeedItem.java new file mode 100644 index 00000000..a1d5da5b --- /dev/null +++ b/core/src/main/java/com/muwire/core/filefeeds/FeedItem.java @@ -0,0 +1,79 @@ +package com.muwire.core.filefeeds; + +import java.util.Objects; + +import com.muwire.core.InfoHash; +import com.muwire.core.Persona; + +public class FeedItem { + + private final Persona publisher; + private final long timestamp; + private final String name; + private final long size; + private final int pieceSize; + private final InfoHash infoHash; + private final int certificates; + private final String comment; + + public FeedItem(Persona publisher, long timestamp, String name, long size, int pieceSize, InfoHash infoHash, + int certificates, String comment) { + super(); + this.publisher = publisher; + this.timestamp = timestamp; + this.name = name; + this.size = size; + this.pieceSize = pieceSize; + this.infoHash = infoHash; + this.certificates = certificates; + this.comment = comment; + } + + public Persona getPublisher() { + return publisher; + } + + public long getTimestamp() { + return timestamp; + } + + public String getName() { + return name; + } + + public long getSize() { + return size; + } + + public int getPieceSize() { + return pieceSize; + } + + public InfoHash getInfoHash() { + return infoHash; + } + + public int getCertificates() { + return certificates; + } + + public String getComment() { + return comment; + } + + @Override + public int hashCode() { + return Objects.hash(publisher, timestamp, name, infoHash); + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof FeedItem)) + return false; + FeedItem other = (FeedItem)o; + return Objects.equals(publisher, other.publisher) && + timestamp == other.timestamp && + Objects.equals(name, other.name) && + Objects.equals(infoHash, other.infoHash); + } +} diff --git a/core/src/main/java/com/muwire/core/filefeeds/InvalidFeedItemException.java b/core/src/main/java/com/muwire/core/filefeeds/InvalidFeedItemException.java new file mode 100644 index 00000000..4c4e430d --- /dev/null +++ b/core/src/main/java/com/muwire/core/filefeeds/InvalidFeedItemException.java @@ -0,0 +1,30 @@ +package com.muwire.core.filefeeds; + +public class InvalidFeedItemException extends Exception { + + public InvalidFeedItemException() { + super(); + } + + public InvalidFeedItemException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + // TODO Auto-generated constructor stub + } + + public InvalidFeedItemException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + + public InvalidFeedItemException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + public InvalidFeedItemException(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + +}