51
51
public void connect() throws StorageConnectException {
52
if (folder == null || !folder.exists() || !folder.canRead() || !folder.canWrite() || !folder.isDirectory())
53
throw new StorageConnectException("Repository folder '"+folder+"' does not exist or is not writable.");
52
if (folder == null || !folder.exists() || !folder.canRead() || !folder.canWrite() || !folder.isDirectory()) {
53
throw new StorageConnectException("Repository folder '"+folder+"' does not exist or is not writable.");
57
58
public void disconnect() throws StorageException {
62
63
public void download(RemoteFile remoteFile, File localFile) throws RemoteFileNotFoundException, StorageException {
65
File repoFile = getRepoFile(remoteFile);
67
if (!repoFile.exists())
68
throw new RemoteFileNotFoundException("No such file in local repository: "+repoFile);
71
File tempLocalFile = config.getCache().createTempFile();
73
FileUtil.copy(repoFile, tempLocalFile, getConnection().getThrottleKbps());
75
// SNM 6/01/11 windows doesn't support rename *onto* another file
76
if(localFile.exists()){
79
tempLocalFile.renameTo(localFile);
81
catch (IOException ex) {
82
throw new StorageException("Unable to copy file "+repoFile+" from local repository to "+localFile, ex);
66
File repoFile = getRepoFile(remoteFile);
68
if (!repoFile.exists()) {
69
throw new RemoteFileNotFoundException("No such file in local repository: "+repoFile);
73
File tempLocalFile = config.getCache().createTempFile("local-tm-download");
74
tempLocalFile.deleteOnExit();
76
FileUtil.copy(repoFile, tempLocalFile, getConnection().getThrottleKbps());
78
// SNM 6/01/11 windows doesn't support rename *onto* another file
79
if (localFile.exists()){
83
if (!tempLocalFile.renameTo(localFile)) {
84
throw new StorageException("Unable to move temp local file "+tempLocalFile+" to "+localFile);
87
tempLocalFile.delete();
89
catch (IOException ex) {
90
throw new StorageException("Unable to copy file "+repoFile+" from local repository to "+localFile, ex);
87
95
public void upload(File localFile, RemoteFile remoteFile) throws LocalFileNotFoundException, StorageException {
90
File repoFile = getRepoFile(remoteFile);
91
File tempRepoFile = new File(FileUtil.getAbsoluteParentDirectory(repoFile)+File.separator+".temp-"+repoFile.getName());
98
File repoFile = getRepoFile(remoteFile);
99
File tempRepoFile = new File(FileUtil.getAbsoluteParentDirectory(repoFile)+File.separator+".temp-"+repoFile.getName());
93
101
// Do not overwrite files!
94
if (repoFile.exists())
102
if (repoFile.exists()) {
97
106
// No such local file
98
if (!localFile.exists())
99
throw new LocalFileNotFoundException("No such file on local disk: "+localFile);
107
if (!localFile.exists()) {
108
throw new LocalFileNotFoundException("No such file on local disk: "+localFile);
102
FileUtil.copy(localFile, tempRepoFile, getConnection().getThrottleKbps());
103
tempRepoFile.renameTo(repoFile);
105
catch (IOException ex) {
106
throw new StorageException("Unable to copy file "+localFile+" to local repository "+repoFile, ex);
112
FileUtil.copy(localFile, tempRepoFile, getConnection().getThrottleKbps());
113
tempRepoFile.renameTo(repoFile);
115
catch (IOException ex) {
116
throw new StorageException("Unable to copy file "+localFile+" to local repository "+repoFile, ex);
111
121
public void delete(RemoteFile remoteFile) throws RemoteFileNotFoundException, StorageException {
114
File repoFile = getRepoFile(remoteFile);
116
if (!repoFile.exists())
124
File repoFile = getRepoFile(remoteFile);
126
if (!repoFile.exists()) {
128
139
public Map<String, RemoteFile> list(final String namePrefix) throws StorageException {
133
if (namePrefix == null) {
134
files = folder.listFiles();
137
files = folder.listFiles(new FilenameFilter() {
138
@Override public boolean accept(File dir, String name) {
139
return name.startsWith(namePrefix); }
144
throw new StorageException("Unable to read local respository "+folder);
146
Map<String, RemoteFile> remoteFiles = new HashMap<String, RemoteFile>();
148
for (File file : files)
149
remoteFiles.put(file.getName(), new RemoteFile(file.getName(), file.length(), file));
144
if (namePrefix == null) {
145
files = folder.listFiles();
148
files = folder.listFiles(new FilenameFilter() {
149
@Override public boolean accept(File dir, String name) {
150
return name.startsWith(namePrefix); }
155
throw new StorageException("Unable to read local respository "+folder);
158
Map<String, RemoteFile> remoteFiles = new HashMap<String, RemoteFile>();
160
for (File file : files) {
161
remoteFiles.put(file.getName(), new RemoteFile(file.getName(), file.length(), file));
151
164
return remoteFiles;