📄 publishtransaction.java
字号:
/*
* Created on Jun 29, 2006 10:16:26 PM
* Copyright (C) 2006 Aelitis, All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* AELITIS, SAS au capital de 46,603.30 euros
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
*/
package com.aelitis.azureus.ui.swt.browser.listener.publish;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.internal.image.FileFormat;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.bouncycastle.util.encoders.Base64;
import org.json.JSONObject;
import com.aelitis.azureus.core.messenger.ClientMessageContext;
import com.aelitis.azureus.ui.swt.browser.msg.BrowserMessage;
import com.aelitis.azureus.ui.swt.browser.txn.Transaction;
import com.aelitis.azureus.ui.swt.utils.ImageResizeException;
import com.aelitis.azureus.ui.swt.utils.ImageResizer;
import org.gudy.azureus2.plugins.PluginInterface;
import org.gudy.azureus2.plugins.download.Download;
import org.gudy.azureus2.plugins.torrent.*;
/**
* Tracks the progress of creating a torrent during the publish process.
*
* @author dharkness
* @created Jul 20, 2006
*/
public class PublishTransaction extends Transaction
{
private static final String ELEMENTS = "elements";
public static final String PUBLISH_ATTRIBUTE_KEY = "DIRECTOR PUBLISH"; //TODO this should really be placed in DirectorPlugin.java
private static final int DEFAULT_IMAGE_BOX_SIZE = 320;
private static final float DEFAULT_JPEG_QUALITY = 0.85f;
private PluginInterface pluginInterface;
private Shell shell;
private LocalHoster hoster;
private TorrentCreator creator;
private TorrentCreatorListener creatorListener;
private File dataFile;
public void setPluginInterface(PluginInterface pluginInterface) {
this.pluginInterface = pluginInterface;
}
public void setShell(Shell shell) {
this.shell = shell;
}
public void setLocalHoster(LocalHoster hoster) {
this.hoster = hoster;
}
/**
* Creates the transaction. Called by the manager.
*
* @param id unique ID assigned by the manager
* @param type passed by manager
* @param context used to access the browser
*/
public PublishTransaction ( int id , String type , ClientMessageContext context ) {
super(id, type, context);
}
/**
* Opens a file dialog so the user can choose the file to torrent.
*/
public void chooseFile ( BrowserMessage message ) {
FileDialog dialog = new FileDialog(shell);
String file = dialog.open();
createTorrentFile(file);
}
/**
* Opens a file dialog so the user can choose the folder to torrent.
*/
public void chooseFolder ( BrowserMessage message ) {
DirectoryDialog dialog = new DirectoryDialog(shell);
String file = dialog.open();
createTorrentFile(file);
}
protected boolean canceling ( ) {
if(creator != null) {
creator.cancel();
creator.removeListener(creatorListener);
creator = null;
//Gudy sendBrowserMessage("torrent","canceled");
}
return true;
}
/**
* Opens a file dialog so the user can choose the image to use as a thumbnail
*/
public void chooseThumbnail(BrowserMessage message) {
final int resize_size[] = {DEFAULT_IMAGE_BOX_SIZE,DEFAULT_IMAGE_BOX_SIZE};
final float image_quality[] = {DEFAULT_JPEG_QUALITY};
JSONObject elements = null; //will be used if several thumbnails are required on a single page
if ( message.isParamObject() ) {
JSONObject parameters = message.getDecodedObject();
try {
resize_size[0] = parameters.getInt("width");
resize_size[1] = parameters.getInt("height");
image_quality[0] = (float) parameters.getDouble("quality");
if (parameters.has(ELEMENTS)){
elements = parameters.getJSONObject(ELEMENTS);
}
} catch(Exception e) {
//Possible bad parameters given, use default values
e.printStackTrace();
}
}
FileDialog dialog = new FileDialog(shell,SWT.OPEN);
dialog.setFilterNames(new String[] {"Image Files"});
dialog.setFilterExtensions(new String[] {"*.jpg;*.jpeg;*.bmp;*.gif;*.png"});
final String fileName = dialog.open();
if(fileName != null) {
//Run async not to block the UI
/*Thread runner =
new Thread("Thumbnail Creator") {
public void run() {*/
try {
sendBrowserMessage("thumb", "start", elements);
File file = new File(fileName);
ResizedImageInfo info = loadAndResizeImage(file,resize_size[0],resize_size[1],image_quality[0]);
if(info == null) {
debug("User canceled image resizing");
sendBrowserMessage("thumb", "clear", elements);
} else {
final String thumbURL = info.url.toString();
debug("Size : " + info.data.length);
final String encoded = new String(Base64.encode(info.data));
JSONObject params = new JSONObject();
params.put("url", thumbURL);
params.put("width", info.width);
params.put("height", info.height);
params.put("data", encoded);
if ( elements != null ){
params.put(ELEMENTS, elements);
}
sendBrowserMessage("thumb", "done", params);
}
}
catch(ImageResizeException e) {
debug("Error resizing image",e);
sendBrowserMessage("thumb", "clear", elements);
JSONObject params = new JSONObject();
params.put("message", e.getMessage());
sendBrowserMessage("page", "error",params);
}
catch (Exception e) {
debug("Error reading file",e);
sendBrowserMessage("thumb", "clear", elements);
JSONObject params = new JSONObject();
params.put("message", "Azureus cannot process this image. Please select another one.");
sendBrowserMessage("page", "error",params);
}
catch (OutOfMemoryError e) {
debug("Error processing the image",e);
sendBrowserMessage("thumb", "clear", elements);
JSONObject params = new JSONObject();
params.put("message", "Azureus cannot process this image (likely reason is that it is too big). Please select another one.");
sendBrowserMessage("page", "error",params);
}
/*}
};
runner.setDaemon(true);
runner.start(); */
}
}
/**
* Pulls the modified torrent from the result web page and saves it locally.
*/
public void torrentIsReady ( BrowserMessage message ) {
torrentIsReady(message.getDecodedObject().getString("torrent"));
}
protected void torrentIsReady ( String strTorrent ) {
try{
strTorrent = strTorrent.replaceAll("\\n", "");
debug( "data file path = [" + dataFile.getPath()+"]" );
debug("Torrent is ready, size = " + strTorrent.length() + ", content (base64) : " + strTorrent);
byte[] torrent_data = Base64.decode(strTorrent);
debug("Torrent Byte Length: " + torrent_data.length /* + ", content : " + new String(torrent_data) */ );
Torrent torrent = pluginInterface.getTorrentManager().createFromBEncodedData(torrent_data);
torrent.setDefaultEncoding();
final Download download = pluginInterface.getDownloadManager().addDownload(torrent, null, dataFile );
//get the "content" attribute for the download
TorrentAttribute attrib = pluginInterface.getTorrentManager().getPluginAttribute( TorrentAttribute.TA_CONTENT_MAP );
Map content_map = download.getMapAttribute( attrib );
if( content_map == null ) {
//System.out.print( "torrentIsReady:: content_map == null" );
content_map = new HashMap();
}
content_map.put( PUBLISH_ATTRIBUTE_KEY, new Long(1) ); //mark this download as "Director published" so we can pick it up later
download.setMapAttribute( attrib, content_map );
download.setForceStart( true );
//Transaction is finished
stop();
}
catch( Throwable t ) {
t.printStackTrace();
}
}
private void torrentCreationFailed(Exception cause) {
debug("Torrent Creation Failed", cause);
sendBrowserMessage("torrent","failed");
JSONObject params = new JSONObject();
params.put("message", "Azureus cannot process this file. Please select another file.");
sendBrowserMessage("page", "error",params);
}
private void createTorrentFile(String file) {
if(file != null) {
dataFile = new File(file);
try {
creator = pluginInterface.getTorrentManager().createFromDataFileEx(dataFile, new URL("http://xxxxxxxxxxxxxxxxx:6969/announce"), false);
creatorListener = new TorrentCreatorListener() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -