protected long getTotalFileSizeSupport(File file) throws TOTorrentException { String name = file.getName(); ///////////////////////////////////////// ///////////////////////////////////// if (name.equals(".") || name.equals("..")) { return (0); } if (!file.exists()) { throw (new TOTorrentException( "TOTorrentCreate: file '" + file.getName() + "' doesn't exist", TOTorrentException.RT_FILE_NOT_FOUND)); } if (file.isFile()) { if (!ignoreFile(name)) { total_file_count++; return (file.length()); } else { return (0); } } else { File[] dir_files = file.listFiles(); if (dir_files == null) { throw (new TOTorrentException( "TOTorrentCreate: directory '" + file.getAbsolutePath() + "' returned error when listing files in it", TOTorrentException.RT_FILE_NOT_FOUND)); } long length = 0; for (int i = 0; i < dir_files.length; i++) { length += getTotalFileSizeSupport(dir_files[i]); } return (length); } }
protected static long getTorrentDataSizeFromFileOrDirSupport(File file) { String name = file.getName(); if (name.equals(".") || name.equals("..")) { return (0); } if (!file.exists()) { return (0); } if (file.isFile()) { return (file.length()); } else { File[] dir_files = file.listFiles(); long length = 0; for (int i = 0; i < dir_files.length; i++) { length += getTorrentDataSizeFromFileOrDirSupport(dir_files[i]); } return (length); } }
protected TOTorrentCreateImpl( File _torrent_base, URL _announce_url, boolean _add_other_hashes, long _piece_length) throws TOTorrentException { super(_torrent_base.getName(), _announce_url, _torrent_base.isFile()); torrent_base = _torrent_base; piece_length = _piece_length; add_other_hashes = _add_other_hashes; }
protected TOTorrentCreateImpl( File _torrent_base, URL _announce_url, boolean _add_other_hashes, long _piece_min_size, long _piece_max_size, long _piece_num_lower, long _piece_num_upper) throws TOTorrentException { super(_torrent_base.getName(), _announce_url, _torrent_base.isFile()); torrent_base = _torrent_base; add_other_hashes = _add_other_hashes; long total_size = calculateTotalFileSize(_torrent_base); piece_length = getComputedPieceSize( total_size, _piece_min_size, _piece_max_size, _piece_num_lower, _piece_num_upper); }
protected void processDir(TOTorrentFileHasher hasher, File dir, Vector encoded, String root) throws TOTorrentException { File[] dir_file_list = dir.listFiles(); if (dir_file_list == null) { throw (new TOTorrentException( "TOTorrentCreate: directory '" + dir.getAbsolutePath() + "' returned error when listing files in it", TOTorrentException.RT_FILE_NOT_FOUND)); } // sort contents so that multiple encodes of a dir always // generate same torrent List file_list = new ArrayList(Arrays.asList(dir_file_list)); Collections.sort(file_list); long offset = 0; for (int i = 0; i < file_list.size(); i++) { File file = (File) file_list.get(i); String file_name = file.getName(); if (!(file_name.equals(".") || file_name.equals(".."))) { if (file.isDirectory()) { if (root.length() > 0) { file_name = root + File.separator + file_name; } processDir(hasher, file, encoded, file_name); } else { if (!ignoreFile(file_name)) { if (root.length() > 0) { file_name = root + File.separator + file_name; } long length = hasher.add(file); TOTorrentFileImpl tf = new TOTorrentFileImpl(this, offset, length, file_name); offset += length; if (add_other_hashes) { byte[] ed2k_digest = hasher.getPerFileED2KDigest(); byte[] sha1_digest = hasher.getPerFileSHA1Digest(); // System.out.println( "file:ed2k = " + ByteFormatter.nicePrint( ed2k_digest, true )); // System.out.println( "file:sha1 = " + ByteFormatter.nicePrint( sha1_digest, true )); tf.setAdditionalProperty("sha1", sha1_digest); tf.setAdditionalProperty("ed2k", ed2k_digest); } encoded.addElement(tf); } } } } }
public void runSupport() { try { new URL(url_str); // determine if this is already a proper URL } catch (Throwable t) { // it's not // //check if the string is just a base32/hex-encoded torrent infohash // // String magnet_uri = UrlUtils.normaliseMagnetURI( url_str ); // // if ( magnet_uri != null ){ // // url_str = magnet_uri; // } } try { url = AddressUtils.adjustURL(new URL(url_str)); String protocol = url.getProtocol().toLowerCase(); // hack here - the magnet download process requires an additional paramter to cause it to // stall on error so the error can be reported // if ( protocol.equals( "magnet" ) || protocol.equals( "dht" )){ // // url = AddressUtils.adjustURL( new URL(url_str+"&pause_on_error=true")); // } for (int i = 0; i < 2; i++) { try { // if ( protocol.equals("https")){ // // // see ConfigurationChecker for SSL client defaults // // HttpsURLConnection ssl_con = (HttpsURLConnection)url.openConnection(); // // // allow for certs that contain IP addresses rather than dns names // // ssl_con.setHostnameVerifier( // new HostnameVerifier() // { // public boolean // verify( // String host, // SSLSession session ) // { // return( true ); // } // }); // // con = ssl_con; // // }else{ // con = (HttpURLConnection) url.openConnection(); // } con.setRequestProperty( "User-Agent", Constants.AZUREUS_NAME + " " + Constants.AZUREUS_VERSION); if (referrer != null && referrer.length() > 0) { con.setRequestProperty("Referer", referrer); } if (request_properties != null) { Iterator it = request_properties.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); // currently this code doesn't support gzip/deflate... if (!key.equalsIgnoreCase("Accept-Encoding")) { con.setRequestProperty(key, value); } } } this.con.connect(); break; // }catch( SSLException e ){ // // if ( i == 0 ){ // // if ( SESecurityManager.installServerCertificates( url ) != null ){ // // // certificate has been installed // // continue; // retry with new certificate // } // } // // throw( e ); // } catch (IOException e) { if (i == 0) { URL retry_url = UrlUtils.getIPV4Fallback(url); if (retry_url != null) { url = retry_url; } else { throw (e); } } } } int response = this.con.getResponseCode(); if (!ignoreReponseCode) { if ((response != HttpURLConnection.HTTP_ACCEPTED) && (response != HttpURLConnection.HTTP_OK)) { this.error(response, Integer.toString(response) + ": " + this.con.getResponseMessage()); return; } } /* Map headerFields = this.con.getHeaderFields(); System.out.println("Header of download of " + url_str); for (Iterator iter = headerFields.keySet().iterator(); iter.hasNext();) { String s = (String) iter.next(); System.out.println(s + ":" + headerFields.get(s)); } */ filename = this.con.getHeaderField("Content-Disposition"); if ((filename != null) && filename .toLowerCase() .matches(".*attachment.*")) // Some code to handle b0rked servers. while (filename.toLowerCase().charAt(0) != 'a') filename = filename.substring(1); if ((filename == null) || !filename.toLowerCase().startsWith("attachment") || (filename.indexOf('=') == -1)) { String tmp = this.url.getFile(); if (tmp.length() == 0 || tmp.equals("/")) { filename = url.getHost(); } else if (tmp.startsWith("?")) { // probably a magnet URI - use the hash // magnet:?xt=urn:sha1:VGC53ZWCUXUWVGX7LQPVZIYF4L6RXSU6 String query = tmp.toUpperCase(); int pos = query.indexOf("XT=URN:SHA1:"); if (pos == -1) { pos = query.indexOf("XT=URN:BTIH:"); } if (pos != -1) { pos += 12; int p2 = query.indexOf("&", pos); if (p2 == -1) { filename = query.substring(pos); } else { filename = query.substring(pos, p2); } } else { filename = "Torrent" + (long) (Math.random() * Long.MAX_VALUE); } filename += ".tmp"; } else { if (tmp.lastIndexOf('/') != -1) tmp = tmp.substring(tmp.lastIndexOf('/') + 1); // remove any params in the url int param_pos = tmp.indexOf('?'); if (param_pos != -1) { tmp = tmp.substring(0, param_pos); } filename = URLDecoder.decode(tmp, Constants.DEFAULT_ENCODING); } } else { filename = filename.substring(filename.indexOf('=') + 1); if (filename.startsWith("\"") && filename.endsWith("\"")) filename = filename.substring(1, filename.lastIndexOf('\"')); filename = URLDecoder.decode(filename, Constants.DEFAULT_ENCODING); // not sure of this piece of logic here but I'm not changing it at the moment File temp = new File(filename); filename = temp.getName(); } filename = FileUtil.convertOSSpecificChars(filename, false); // directoryname = // COConfigurationManager.getDirectoryParameter("General_sDefaultTorrent_Directory"); // boolean useTorrentSave = COConfigurationManager.getBooleanParameter("Save Torrent // Files"); directoryname = "D:\\Development\\testDownloads\\"; boolean useTorrentSave = true; if (file_str != null) { // not completely sure about the whole logic in this block File temp = new File(file_str); // if we're not using a default torrent save dir if (!useTorrentSave || directoryname.length() == 0) { // if it's already a dir if (temp.isDirectory()) { // use it directoryname = temp.getCanonicalPath(); } // it's a file else { // so use its parent dir directoryname = temp.getCanonicalFile().getParent(); } } // if it's a file if (!temp.isDirectory()) { // set the file name filename = temp.getName(); } } // what would happen here if directoryname == null and file_str == null?? this.state = STATE_INIT; this.notifyListener(); } catch (java.net.MalformedURLException e) { this.error(0, "Exception while parsing URL '" + url + "':" + e.getMessage()); } catch (java.net.UnknownHostException e) { this.error( 0, "Exception while initializing download of '" + url + "': Unknown Host '" + e.getMessage() + "'"); } catch (java.io.IOException ioe) { this.error(0, "I/O Exception while initializing download of '" + url + "':" + ioe.toString()); } catch (Throwable e) { this.error(0, "Exception while initializing download of '" + url + "':" + e.toString()); } if (this.state == STATE_ERROR) { return; } try { final boolean status_reader_run[] = {true}; this.state = STATE_START; notifyListener(); this.state = STATE_DOWNLOADING; notifyListener(); Thread status_reader = new AEThread("TorrentDownloader:statusreader") { public void runSupport() { boolean changed_status = false; while (true) { try { Thread.sleep(100); try { this_mon.enter(); if (!status_reader_run[0]) { break; } } finally { this_mon.exit(); } String s = con.getResponseMessage(); if (!s.equals(getStatus())) { if (!s.toLowerCase().startsWith("error:")) { if (s.toLowerCase().indexOf("alive") != -1) { if (percentDone < 10) { percentDone++; } } int pos = s.indexOf('%'); if (pos != -1) { int i; for (i = pos - 1; i >= 0; i--) { char c = s.charAt(i); if (!Character.isDigit(c) && c != ' ') { i++; break; } } try { percentDone = Integer.parseInt(s.substring(i, pos).trim()); } catch (Throwable e) { } } setStatus(s); } else { error(con.getResponseCode(), s.substring(6)); } changed_status = true; } } catch (Throwable e) { break; } } if (changed_status) { setStatus(""); } } }; status_reader.setDaemon(true); status_reader.start(); InputStream in; try { in = this.con.getInputStream(); } catch (FileNotFoundException e) { if (ignoreReponseCode) { in = this.con.getErrorStream(); } else { throw e; } } finally { try { this_mon.enter(); status_reader_run[0] = false; } finally { this_mon.exit(); } } // handle some servers that return gzip'd torrents even though we don't request it! String encoding = con.getHeaderField("content-encoding"); if (encoding != null) { if (encoding.equalsIgnoreCase("gzip")) { in = new GZIPInputStream(in); } else if (encoding.equalsIgnoreCase("deflate")) { in = new InflaterInputStream(in); } } if (this.state != STATE_ERROR) { this.file = new File(this.directoryname, filename); boolean useTempFile = false; try { this.file.createNewFile(); useTempFile = !this.file.exists(); } catch (Throwable t) { useTempFile = true; } if (useTempFile) { this.file = File.createTempFile("AZU", ".torrent", new File(this.directoryname)); this.file.createNewFile(); } FileOutputStream fileout = new FileOutputStream(this.file, false); bufBytes = 0; int size = (int) UrlUtils.getContentLength(con); this.percentDone = -1; do { if (this.cancel) { break; } try { bufBytes = in.read(buf); this.readTotal += bufBytes; if (size != 0) { this.percentDone = (100 * this.readTotal) / size; } notifyListener(); } catch (IOException e) { } if (bufBytes > 0) { fileout.write(buf, 0, bufBytes); } } while (bufBytes > 0); in.close(); fileout.flush(); fileout.close(); if (this.cancel) { this.state = STATE_CANCELLED; if (deleteFileOnCancel) { this.cleanUpFile(); } } else { if (this.readTotal <= 0) { this.error(0, "No data contained in '" + this.url.toString() + "'"); return; } // if the file has come down with a not-so-useful name then we try to rename // it to something more useful try { if (!filename.toLowerCase().endsWith(".torrent")) { TOTorrent torrent = TorrentUtils.readFromFile(file, false); String name = TorrentUtils.getLocalisedName(torrent) + ".torrent"; File new_file = new File(directoryname, name); if (file.renameTo(new_file)) { filename = name; file = new_file; } } } catch (Throwable e) { Debug.printStackTrace(e); } // TorrentUtils.setObtainedFrom( file, original_url ); this.state = STATE_FINISHED; } this.notifyListener(); } } catch (Exception e) { if (!cancel) { Debug.out("'" + this.directoryname + "' '" + filename + "'", e); } this.error(0, "Exception while downloading '" + this.url.toString() + "':" + e.getMessage()); } }