use of com.biglybt.core.torrent.TOTorrentCreator in project BiglyBT by BiglySoftware.
the class SubscriptionImpl method checkPublish.
protected void checkPublish() {
synchronized (this) {
if (destroyed) {
return;
}
if (isSingleton()) {
return;
}
if (!isSubscribed()) {
return;
}
if (popularity > 100) {
if (lws_skip_check == 2) {
return;
} else if (lws_skip_check == 0) {
if (RandomUtils.nextInt((int) ((popularity + 99) / 100)) == 0) {
lws_skip_check = 1;
} else {
lws_skip_check = 2;
return;
}
}
}
if (hash != null) {
boolean create = false;
if (lws == null) {
create = true;
} else {
if (!Arrays.equals(lws.getHash().getBytes(), hash)) {
lws.remove();
create = true;
}
}
if (create) {
try {
File original_data_location = manager.getVuzeFile(this);
if (original_data_location.exists()) {
// make a version based filename to avoid issues regarding multiple
// versions
final File versioned_data_location = new File(original_data_location.getParent(), original_data_location.getName() + "." + getVersion());
if (!versioned_data_location.exists()) {
if (!FileUtil.copyFile(original_data_location, versioned_data_location)) {
throw (new Exception("Failed to copy file to '" + versioned_data_location + "'"));
}
}
lws = LightWeightSeedManager.getSingleton().add(getName(), new HashWrapper(hash), TorrentUtils.getDecentralisedEmptyURL(), versioned_data_location, isAnonymous() ? AENetworkClassifier.AT_I2P : AENetworkClassifier.AT_PUBLIC, new LightWeightSeedAdapter() {
@Override
public TOTorrent getTorrent(byte[] hash, URL announce_url, File data_location) throws Exception {
log("Generating light-weight torrent: hash=" + ByteFormatter.encodeString(hash));
TOTorrentCreator creator = TOTorrentFactory.createFromFileOrDirWithFixedPieceLength(data_location, announce_url, 256 * 1024);
TOTorrent t = creator.create();
t.setHashOverride(hash);
return (t);
}
});
}
} catch (Throwable e) {
log("Failed to create light-weight-seed", e);
}
}
}
}
}
use of com.biglybt.core.torrent.TOTorrentCreator in project BiglyBT by BiglySoftware.
the class ShareResourceFileOrDirImpl method createTorrent.
protected void createTorrent() throws ShareException {
try {
manager.reportCurrentTask((item == null ? "Creating" : "Re-creating").concat(" torrent for '").concat(file.toString()).concat("'"));
URL[] urls = manager.getAnnounceURLs();
TOTorrentCreator creator = TOTorrentFactory.createFromFileOrDirWithComputedPieceLength(file, urls[0], manager.getAddHashes());
creator.addListener(manager);
TOTorrent to_torrent;
try {
manager.setTorrentCreator(creator);
to_torrent = creator.create();
} finally {
manager.setTorrentCreator(null);
}
if (personal_key != null) {
Map map = to_torrent.serialiseToMap();
Map info = (Map) map.get("info");
info.put("az_salt", personal_key);
to_torrent = TOTorrentFactory.deserialiseFromMap(map);
}
LocaleTorrentUtil.setDefaultTorrentEncoding(to_torrent);
for (int i = 1; i < urls.length; i++) {
TorrentUtils.announceGroupsInsertLast(to_torrent, new URL[] { urls[i] });
}
String comment = COConfigurationManager.getStringParameter("Sharing Torrent Comment").trim();
boolean private_torrent = COConfigurationManager.getBooleanParameter("Sharing Torrent Private");
boolean dht_backup_enabled = COConfigurationManager.getBooleanParameter("Sharing Permit DHT");
TorrentAttribute ta_props = TorrentManagerImpl.getSingleton().getAttribute(TorrentAttribute.TA_SHARE_PROPERTIES);
String props = getAttribute(ta_props);
if (props != null) {
StringTokenizer tok = new StringTokenizer(props, ";");
while (tok.hasMoreTokens()) {
String token = tok.nextToken();
int pos = token.indexOf('=');
if (pos == -1) {
Debug.out("ShareProperty invalid: " + props);
} else {
String lhs = token.substring(0, pos).trim().toLowerCase();
String rhs = token.substring(pos + 1).trim().toLowerCase();
boolean set = rhs.equals("true");
if (lhs.equals("private")) {
private_torrent = set;
} else if (lhs.equals("dht_backup")) {
dht_backup_enabled = set;
} else if (lhs.equals("comment")) {
comment = rhs;
} else {
Debug.out("ShareProperty invalid: " + props);
break;
}
}
}
}
if (comment.length() > 0) {
to_torrent.setComment(comment);
}
TorrentUtils.setDHTBackupEnabled(to_torrent, dht_backup_enabled);
TorrentUtils.setPrivate(to_torrent, private_torrent);
if (TorrentUtils.isDecentralised(to_torrent)) {
TorrentUtils.setDecentralised(to_torrent);
}
if (COConfigurationManager.getBooleanParameter("Sharing Disable RCM")) {
TorrentUtils.setFlag(to_torrent, TorrentUtils.TORRENT_FLAG_DISABLE_RCM, true);
}
DownloadManagerState download_manager_state = DownloadManagerStateFactory.getDownloadState(to_torrent);
TorrentUtils.setResumeDataCompletelyValid(download_manager_state);
download_manager_state.save();
if (item == null) {
byte[] fingerprint = getFingerPrint();
item = new ShareItemImpl(this, fingerprint, new TorrentImpl(to_torrent));
} else {
item.setTorrent(new TorrentImpl(to_torrent));
item.writeTorrent();
}
} catch (TOTorrentException e) {
if (e.getReason() == TOTorrentException.RT_CANCELLED) {
throw (new ShareException("ShareResource: Operation cancelled", e));
} else {
throw (new ShareException("ShareResource: Torrent create failed", e));
}
} catch (Throwable e) {
throw (new ShareException("ShareResource: Torrent create failed", e));
}
}
use of com.biglybt.core.torrent.TOTorrentCreator in project BiglyBT by BiglySoftware.
the class Create method execute.
@Override
public void execute(String commandName, final ConsoleInput ci, List<String> args) {
if (args.size() < 3) {
printHelp(ci.out, args);
return;
}
File input_file = new File(args.get(0));
if (!input_file.exists()) {
ci.out.println("Input file '" + input_file.getAbsolutePath() + "' doesn't exist");
return;
}
File output_file = new File(args.get(1));
if (output_file.exists()) {
ci.out.println("Output file '" + input_file.getAbsolutePath() + "' already exists");
return;
}
List<URL> urls = new ArrayList<>();
for (int i = 2; i < args.size(); i++) {
try {
urls.add(new URL(args.get(i)));
} catch (Throwable e) {
ci.out.println("Invalid URL: " + args.get(i));
return;
}
}
try {
TOTorrentCreator creator = TOTorrentFactory.createFromFileOrDirWithComputedPieceLength(input_file, urls.get(0));
creator.addListener(new TOTorrentProgressListener() {
@Override
public void reportProgress(int percent_complete) {
ci.out.println("\t\t" + percent_complete + "%");
}
@Override
public void reportCurrentTask(String task_description) {
ci.out.println("\t" + task_description);
}
});
TOTorrent torrent = creator.create();
if (urls.size() > 1) {
TOTorrentAnnounceURLGroup group = torrent.getAnnounceURLGroup();
TOTorrentAnnounceURLSet[] sets = new TOTorrentAnnounceURLSet[urls.size()];
for (int i = 0; i < urls.size(); i++) {
sets[i] = group.createAnnounceURLSet(new URL[] { urls.get(i) });
ci.out.println("\tAdded URL '" + urls.get(i) + "'");
}
group.setAnnounceURLSets(sets);
}
torrent.serialiseToBEncodedFile(output_file);
ci.out.println("\tTorrent written to '" + output_file + "'");
} catch (Throwable e) {
ci.out.println("Failed to create torrent: " + Debug.getNestedExceptionMessage(e));
}
}
use of com.biglybt.core.torrent.TOTorrentCreator in project BiglyBT by BiglySoftware.
the class SubscriptionSelectedContent method getTorrent.
@Override
public TOTorrent getTorrent() {
synchronized (this) {
if (torrent == null) {
try {
VuzeFile vf = subs.getVuzeFile();
if (vf != null) {
File f1 = AETemporaryFileHandler.createTempFile();
File f = new File(f1.getParent(), "Update Vuze to access this share_" + f1.getName());
f1.delete();
try {
vf.write(f);
TOTorrentCreator cr = TOTorrentFactory.createFromFileOrDirWithComputedPieceLength(f, new URL("dht://"));
TOTorrent temp = cr.create();
Map vuze_map = vf.exportToMap();
Map torrent_map = temp.serialiseToMap();
torrent_map.putAll(vuze_map);
torrent = TOTorrentFactory.deserialiseFromMap(torrent_map);
} finally {
f.delete();
}
}
} catch (Throwable e) {
Debug.out(e);
}
}
}
return (torrent);
}
Aggregations