⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadmanagerdefaultpaths.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.gudy.azureus2.core3.download.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.gudy.azureus2.core3.download.DownloadManager;
import org.gudy.azureus2.core3.download.DownloadManagerState;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.logging.*;
import org.gudy.azureus2.core3.util.FileUtil;

public class DownloadManagerDefaultPaths {

    private final static MovementInformation[] COMPLETION_DETAILS;
    private final static MovementInformation[] REMOVAL_DETAILS;
    private final static MovementInformation[] UPDATE_FOR_MOVE_DETAILS;
    private final static MovementInformation[] UPDATE_FOR_LOGIC_DETAILS;
    private final static TargetSpecification[] DEFAULT_DIRS;

    private final static String STATE_INCOMPLETE = "incomplete download";
    private final static String STATE_COMPLETE_DND = "dnd-complete download";
    private final static String STATE_COMPLETE = "fully-complete download";

    private final static String SUBDIR_PARAM = "File.move.subdir_is_default";


    static {
        SourceSpecification source;
        TargetSpecification dest;
        TransferSpecification trans;
        MovementInformation mi_1, mi_2;

        /**
         * There are three sets of directories that we consider a "default"
         * directory (perhaps it should just be two):
         *
         * - default save dir
         * - completed save dir
         * - removed save dir
         */
        DEFAULT_DIRS = new TargetSpecification[3];
        dest = new TargetSpecification();
        dest.setBoolean("enabled", true);
        dest.setString("target", "Default save path");
        dest.setContext("default save dir");
        DEFAULT_DIRS[0] = dest;


        // First - download completion details.
		source = new SourceSpecification();
		source.setBoolean("default dir", "Move Only When In Default Save Dir");
		source.setBoolean("default subdir", SUBDIR_PARAM);
		source.setBoolean("persistent only", true);
		source.setBoolean("check exclusion flag", true);
		source.setBoolean("check completion flag", true);
		source.setBoolean(STATE_INCOMPLETE, false);
		source.setBoolean(STATE_COMPLETE_DND, false);
		source.setBoolean(STATE_COMPLETE, true); // Only handle fully complete downloads at moment.

		dest = new TargetSpecification();
		dest.setBoolean("enabled", "Move Completed When Done");
		dest.setString("target", "Completed Files Directory");
		dest.setContext("completed files dir");

		trans = new TransferSpecification();
		trans.setBoolean("torrent", "Move Torrent When Done");

		mi_1 = new MovementInformation(source, dest, trans, "Move on completion");
		COMPLETION_DETAILS = new MovementInformation[] {mi_1};
		DEFAULT_DIRS[1] = dest;

		// Next - download removal details.
		source = new SourceSpecification();
		source.setBoolean("default dir", "File.move.download.removed.only_in_default");
		source.setBoolean("default subdir", SUBDIR_PARAM);
		source.setBoolean("persistent only", true);
		source.setBoolean("check exclusion flag", true);
		source.setBoolean("check completion flag", false);
		source.setBoolean(STATE_INCOMPLETE, false);
		source.setBoolean(STATE_COMPLETE_DND, "File.move.download.removed.move_partial");
		source.setBoolean(STATE_COMPLETE, true);

		dest = new TargetSpecification();
		dest.setBoolean("enabled", "File.move.download.removed.enabled");
		dest.setString("target", "File.move.download.removed.path");
		dest.setContext("removed files dir");

		trans = new TransferSpecification();
		trans.setBoolean("torrent", "File.move.download.removed.move_torrent");

		mi_1 = new MovementInformation(source, dest, trans, "Move on removal");
		REMOVAL_DETAILS = new MovementInformation[] {mi_1};
		DEFAULT_DIRS[2] = dest;

	    /**
	     * Next - updating the current path (complete dl's first)
	     * 
	     * We instantiate the "update incomplete download" source first, and then
	     * we instantiate the "update complete download", but when we process, we
	     * will do the complete download bit first.
	     * 
	     * We do this, because in the "update incomplete download" section, completed
	     * downloads are enabled for it. And the reason it is, is because this will
	     * allow the code to behave properly if move on completion is not enabled.
	     *
	     * Complete downloads apply to this bit, just in case the "move on completion"
	     * section isn't active.
	     */
		source = new SourceSpecification();
		source.updateSettings(COMPLETION_DETAILS[0].source.getSettings());
		source.setBoolean("default dir", true);

		mi_1 = new MovementInformation(source, COMPLETION_DETAILS[0].target,
				COMPLETION_DETAILS[0].transfer, "Update completed download");
		
		// Now incomplete downloads. We have to define completely new settings for
		// it, since we've never defined it before.
		source = new SourceSpecification();
		source.setBoolean("default dir", true); // Must be in default directory to update.
		source.setBoolean("default subdir", SUBDIR_PARAM);
		source.setBoolean("persistent only", true);
		source.setBoolean("check exclusion flag", true);
		source.setBoolean("check completion flag", false);
		source.setBoolean(STATE_INCOMPLETE, true);
		source.setBoolean(STATE_COMPLETE_DND, true);
		source.setBoolean(STATE_COMPLETE, true);

		dest = new TargetSpecification();
		dest.setBoolean("enabled", true);
		dest.setString("target", "Default save path");

		trans = new TransferSpecification();
		trans.setBoolean("torrent", false);

        // Rest of the settings are the same.
		mi_2 = new MovementInformation(source, dest, trans, "Update incomplete download");
		UPDATE_FOR_MOVE_DETAILS = new MovementInformation[] {mi_1, mi_2};

		/**
		 * Now we have a copy of the exact same settings for updates, except we
		 * disable the logic regarding default directory requirements.
		 */
		UPDATE_FOR_LOGIC_DETAILS = new MovementInformation[UPDATE_FOR_MOVE_DETAILS.length];
		for (int i=0; i<UPDATE_FOR_MOVE_DETAILS.length; i++) {
			MovementInformation mi = UPDATE_FOR_MOVE_DETAILS[i];
		    source = new SourceSpecification();
		    source.updateSettings(mi.source.getSettings());
		    source.setBoolean("default dir", false);
		    source.setBoolean("persistent only", false);
		    source.setBoolean("check exclusion flag", false);
		    UPDATE_FOR_LOGIC_DETAILS[i] = new MovementInformation(source,
		        mi.target, mi.transfer, mi.title.replaceAll("Update", "Calculate path for"));
	    }

    }
    
    private static String normaliseRelativePathPart(String name) {
    	name = name.trim();
    	if (name.length() == 0) {return "";}
    	if (name.equals(".") || name.equals("..")) {
    		return null;
    	}
    	return FileUtil.convertOSSpecificChars(name).trim();
    }
    
    public static File normaliseRelativePath(File path) {
    	if (path.isAbsolute()) {return null;}
    	
    	File parent = path.getParentFile();
    	String child_name = normaliseRelativePathPart(path.getName());
    	if (child_name == null) {
    		return null;
    	}
    	
    	//  Simple one-level path.
    	if (parent == null) {
    		return new File(child_name);
    	}
    	
    	ArrayList parts = new ArrayList();
    	parts.add(child_name);
    	
    	String filepart = null;
    	while (parent != null) {
    		filepart = normaliseRelativePathPart(parent.getName());
    		if (filepart == null) {return null;}
    		else if (filepart.length()==0) {/* continue */}
    		else {parts.add(0, filepart);} 
    		parent = parent.getParentFile();
    	}
    	
    	StringBuffer sb = new StringBuffer((String)parts.get(0));
    	for (int i=1; i<parts.size(); i++) {
    		sb.append(File.separatorChar);
    		sb.append(parts.get(i));
    	}
    	
    	return new File(sb.toString());
    }
    	
    private static File[] getDefaultDirs(LogRelation lr) {
		List results = new ArrayList();
		File location = null;
		TargetSpecification ts = null;
		for (int i=0; i<DEFAULT_DIRS.length; i++) {
			ts = (TargetSpecification)DEFAULT_DIRS[i];
			location = ts.getTarget(null, lr, ts);
			if (location != null) {
				results.add(location);
			}
		}
		return (File[])results.toArray(new File[results.size()]);
	}

	private static String getStateDescriptor(DownloadManager dm) {
		if (dm.isDownloadComplete(true)) {return STATE_COMPLETE;}
		else if (dm.isDownloadComplete(false)) {return STATE_COMPLETE_DND;}
		else {return STATE_INCOMPLETE;}
	}

    // Helper log functions.
	private static void logInfo(String message, LogRelation lr) {
		if (lr == null) {return;}
		if (!Logger.isEnabled()) {return;}
		Logger.log(new LogEvent(lr, LogIDs.CORE, LogEvent.LT_INFORMATION, message));
	}

	private static void logWarn(String message, LogRelation lr) {
		if (lr == null) {return;}
		if (!Logger.isEnabled()) {return;}
		Logger.log(new LogEvent(lr, LogIDs.CORE, LogEvent.LT_WARNING, message));
	}

	private static String describe(DownloadManager dm, ContextDescriptor cs) {
		if (cs == null) {
			if (dm == null) {return "";}
			return "\"" + dm.getDisplayName() + "\"";
		}
		if (dm == null) {
			return "\"" + cs.getContext() + "\"";
		}
		return "\"" + dm.getDisplayName() + "\" with regard to \"" + cs.getContext() + "\"";
	}

    /**
     * This does the guts of determining appropriate file paths.
     */
    private static TransferDetails determinePaths(DownloadManager dm, MovementInformation mi) {
		LogRelation lr = (dm instanceof LogRelation) ? (LogRelation)dm : null;
		boolean proceed = mi.source.matchesDownload(dm, lr, mi);
		if (!proceed) {
			logInfo("Cannot consider " + describe(dm, mi) +

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -