use of com.biglybt.core.vuzefile.VuzeFile in project BiglyBT by BiglySoftware.
the class TorrentOpener method mergeFileIntoTorrentInfo.
/**
* Creates a TorrentInfo from a file. Prompts user if the file is invalid,
* torrent already exists
*
* @param sFileName
* @param sOriginatingLocation
* @return
* @since 5.0.0.1
*/
// TODO: i18n
public static boolean mergeFileIntoTorrentInfo(String sFileName, final String sOriginatingLocation, TorrentOpenOptions torrentOptions) {
TOTorrent torrent = null;
File torrentFile;
boolean bDeleteFileOnCancel = false;
// actually made a copy.
try {
if (sFileName.startsWith("file://localhost/")) {
sFileName = UrlUtils.decode(sFileName.substring(16));
}
final File fOriginal = new File(sFileName);
if (!fOriginal.isFile() || !fOriginal.exists()) {
UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", fOriginal.toString(), new String[] { UrlUtils.decode(sOriginatingLocation), "Not a File" });
return false;
}
if (fOriginal.length() > TorrentUtils.MAX_TORRENT_FILE_SIZE) {
UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", fOriginal.toString(), new String[] { UrlUtils.decode(sOriginatingLocation), "Too large to be a torrent" });
return false;
}
torrentFile = TorrentUtils.copyTorrentFileToSaveDir(fOriginal, true);
bDeleteFileOnCancel = !fOriginal.equals(torrentFile);
// TODO if the files are still equal, and it isn't in the save
// dir, we should copy it to a temp file in case something
// re-writes it. No need to copy a torrent coming from the
// downloader though..
} catch (IOException e1) {
// Use torrent in wherever it is and hope for the best
// XXX Should error instead?
Debug.out(e1);
torrentFile = new File(sFileName);
}
VuzeFileHandler vfh = VuzeFileHandler.getSingleton();
VuzeFile vf = vfh.loadVuzeFile(torrentFile);
if (vf != null) {
vfh.handleFiles(new VuzeFile[] { vf }, VuzeFileComponent.COMP_TYPE_NONE);
return false;
}
if (RSSUtils.isRSSFeed(torrentFile)) {
boolean done = false;
try {
URL url = new URL(sOriginatingLocation);
UIManager ui_manager = StaticUtilities.getUIManager(10 * 1000);
if (ui_manager != null) {
String details = MessageText.getString("subscription.request.add.message", new String[] { sOriginatingLocation });
long res = ui_manager.showMessageBox("subscription.request.add.title", "!" + details + "!", UIManagerEvent.MT_YES | UIManagerEvent.MT_NO);
if (res == UIManagerEvent.MT_YES) {
SubscriptionManager sm = PluginInitializer.getDefaultInterface().getUtilities().getSubscriptionManager();
sm.requestSubscription(url);
done = true;
}
}
} catch (Throwable e) {
Debug.out(e);
}
if (done) {
if (bDeleteFileOnCancel) {
torrentFile.delete();
}
return false;
}
}
// Do a quick check to see if it's a torrent
if (!TorrentUtil.isFileTorrent(sOriginatingLocation, torrentFile, torrentFile.getName(), !torrentOptions.getHideErrors())) {
if (bDeleteFileOnCancel) {
torrentFile.delete();
}
return false;
}
// Load up the torrent, see it it's real
try {
torrent = TorrentUtils.readFromFile(torrentFile, false);
} catch (final TOTorrentException e) {
UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", Debug.getStackTrace(e), new String[] { sOriginatingLocation, e.getMessage() });
if (bDeleteFileOnCancel)
torrentFile.delete();
return false;
}
if (bDeleteFileOnCancel) {
torrentOptions.setDeleteFileOnCancel(bDeleteFileOnCancel);
}
torrentOptions.sFileName = torrentFile.getAbsolutePath();
torrentOptions.setTorrent(torrent);
torrentOptions.sOriginatingLocation = sOriginatingLocation;
return torrentOptions.getTorrent() != null;
}
use of com.biglybt.core.vuzefile.VuzeFile in project BiglyBT by BiglySoftware.
the class BiglyBTURLConnection method connect.
@Override
public void connect() throws IOException {
String str = url.toExternalForm();
int pos = str.indexOf("body=");
if (pos >= 0) {
str = str.substring(pos + 5);
byte[] bytes = str.getBytes(Constants.BYTE_ENCODING);
VuzeFile vf = VuzeFileHandler.getSingleton().loadVuzeFile(bytes);
if (vf == null) {
throw (new IOException("Invalid biglybt url"));
}
input_stream = new ByteArrayInputStream(bytes);
} else {
String host = url.getHost();
String httpsURL;
if (host.equalsIgnoreCase("install-plugin") && url.getPath().length() > 1) {
String plugin_id = url.getPath().substring(1);
httpsURL = Constants.PLUGINS_WEB_SITE + "getplugin.php?plugin=" + plugin_id + "&" + SFPluginDetailsLoaderImpl.getBaseUrlParams();
} else if (host.contains(".")) {
httpsURL = "https" + str.substring("biglybt".length());
} else {
throw (new IOException("Invalid biglybt url"));
}
ResourceDownloader rd = StaticUtilities.getResourceDownloaderFactory().create(new URL(httpsURL));
try {
if (ONLY_VUZE_FILE) {
VuzeFile vf = VuzeFileHandler.getSingleton().loadVuzeFile(rd.download());
if (vf == null) {
throw (new IOException("Invalid biglybt url"));
}
boolean hasPlugin = false;
VuzeFileComponent[] components = vf.getComponents();
for (VuzeFileComponent component : components) {
if (component.getType() == VuzeFileComponent.COMP_TYPE_PLUGIN) {
hasPlugin = true;
break;
}
}
if (!hasPlugin) {
throw (new IOException("Biglybt url does not contain plugin"));
}
input_stream = new ByteArrayInputStream(vf.exportToBytes());
} else {
input_stream = rd.download();
}
} catch (ResourceDownloaderException e) {
throw new IOException(e);
}
}
}
use of com.biglybt.core.vuzefile.VuzeFile in project BiglyBT by BiglySoftware.
the class SearchUtils method importFromClipboard.
private static void importFromClipboard() {
final Shell shell = Utils.findAnyShell();
shell.getDisplay().asyncExec(new AERunnable() {
@Override
public void runSupport() {
try {
Clipboard clipboard = new Clipboard(Display.getDefault());
String text = (String) clipboard.getContents(TextTransfer.getInstance());
clipboard.dispose();
if (text != null) {
InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
try {
VuzeFileHandler vfh = VuzeFileHandler.getSingleton();
VuzeFile vf = vfh.loadVuzeFile(is);
if (vf != null) {
vfh.handleFiles(new VuzeFile[] { vf }, VuzeFileComponent.COMP_TYPE_NONE);
}
} finally {
is.close();
}
}
} catch (Throwable e) {
Debug.out(e);
}
}
});
}
use of com.biglybt.core.vuzefile.VuzeFile in project BiglyBT by BiglySoftware.
the class PluginInstallerImpl method extractFromVuzeFile.
private File extractFromVuzeFile(File file) throws PluginException {
VuzeFile vf = VuzeFileHandler.getSingleton().loadVuzeFile(file);
VuzeFileComponent[] comps = vf.getComponents();
for (int j = 0; j < comps.length; j++) {
VuzeFileComponent comp = comps[j];
if (comp.getType() == VuzeFileComponent.COMP_TYPE_PLUGIN) {
try {
Map content = comp.getContent();
String id = new String((byte[]) content.get("id"), "UTF-8");
String version = new String((byte[]) content.get("version"), "UTF-8");
String suffix = ((Long) content.get("is_jar")).longValue() == 1 ? "jar" : "zip";
byte[] plugin_file = (byte[]) content.get("file");
File temp_dir = AETemporaryFileHandler.createTempDir();
File temp_file = new File(temp_dir, id + "_" + version + "." + suffix);
FileUtil.copyFile(new ByteArrayInputStream(plugin_file), temp_file);
return (temp_file);
} catch (Throwable e) {
throw (new PluginException("Not a valid Vuze file", e));
}
}
}
return (file);
}
use of com.biglybt.core.vuzefile.VuzeFile 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