Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3b8fcc7efeb44fb3354f829777c5217adbcd0429 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
 * Copyright (c) 2018 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.core.data;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.function.Predicate;

import org.eclipse.core.runtime.Assert;
import org.eclipse.mylyn.commons.core.CoreUtil;
import org.eclipse.mylyn.tasks.core.ITask;

/**
 * Encapsulates file-related operations of TaskDataManager
 */
public class TaskDataFileManager {

	private static final String ENCODING_UTF_8 = "UTF-8"; //$NON-NLS-1$

	private static final String EXTENSION = ".zip"; //$NON-NLS-1$

	private static final String FOLDER_TASKS = "tasks"; //$NON-NLS-1$

	private static final String FOLDER_DATA = "offline"; //$NON-NLS-1$

	private static final String FOLDER_TASKS_1_0 = "offline"; //$NON-NLS-1$

	private static final int FILENAME_MAX_LEN = 255 - EXTENSION.length(); // 255 is an OS limit for file name

	private String dataPath;

	public String getDataPath() {
		return dataPath;
	}

	public void setDataPath(String dataPath) {
		this.dataPath = dataPath;
	}

	public File getFile(ITask task, String kind) {
		return getFile(task.getRepositoryUrl(), task, kind);
	}

	public File getFile(String repositoryUrl, ITask task, String kind) {
		File path = getDirectory(repositoryUrl, task);
		String fileName = getFileName(task, path);
		return new File(path, fileName + EXTENSION);
	}

	private File getDirectory(String repositoryUrl, ITask task) {
		Assert.isNotNull(dataPath);
		String repositoryPath = task.getConnectorKind() + "-" + CoreUtil.asFileName(repositoryUrl); //$NON-NLS-1$
		return new File(dataPath + File.separator + FOLDER_TASKS + File.separator + repositoryPath + File.separator
				+ FOLDER_DATA);
	}

	private String getFileName(ITask task, File path) {
		return getFileName(task, filename -> new File(path, filename + EXTENSION).exists());
	}

	// the method is made protected for unit testing
	protected String getFileName(ITask task, Predicate<String> fileExists) {
		String encodedFileName = CoreUtil.asFileName(task.getTaskId());

		// for backwards-compatibility with versions that always encoded file names,
		// we will use an encoded name if the file with an encoded name already exists
		if (fileExists.test(encodedFileName)) {
			return encodedFileName;
		}

		// if file with encoded name does not exist, we will only encode file name if it is required
		String fileName;
		if (requiresEncoding(task.getTaskId())) {
			fileName = encodedFileName;
		} else {
			fileName = task.getTaskId();
		}

		// trim the file name if it is too long
		return trimFilenameIfRequired(fileName);
	}

	/**
	 * Checks if input contains characters other than ones returned by {@link CoreUtil.asFileName}
	 *
	 * @param fileName
	 * @return true or false
	 */
	private boolean requiresEncoding(String fileName) {
		return !fileName.matches("^[a-zA-Z0-9%\\.]+$"); //$NON-NLS-1$
	}

	private String trimFilenameIfRequired(String filename) {
		if (filename.length() > FILENAME_MAX_LEN) {
			// replace a long file name with a shorter name + the hash
			String hashCode = getHashCode(filename);
			return filename.substring(0, FILENAME_MAX_LEN - hashCode.length() - 1) + "." + hashCode; //$NON-NLS-1$
		}

		return filename;
	}

	private String getHashCode(String text) {
		return Integer.toUnsignedString(text.hashCode());
	}

	public File getFile10(ITask task, String kind) {
		try {
			String pathName = URLEncoder.encode(task.getRepositoryUrl(), ENCODING_UTF_8);
			String fileName = task.getTaskId() + EXTENSION;
			File path = new File(dataPath + File.separator + FOLDER_TASKS_1_0, pathName);
			return new File(path, fileName);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}

	}
}

Back to the top