-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropBoxUtilities.java
203 lines (183 loc) · 7.73 KB
/
DropBoxUtilities.java
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.nio.*;
import java.nio.file.*;
import java.io.*;
import java.util.*;
import java.time.format.*;
import java.time.*;
import java.util.stream.*;
import com.dropbox.core.*;
import com.dropbox.core.v2.*;
import com.dropbox.core.v2.files.*;
import com.dropbox.core.v2.users.*;
// import com.dropbox.core.v2.users.FullAccount;
class DropBoxUtilities
{
String API_KEY;
DbxClientV2 client;
DbxRequestConfig config;
public DropBoxUtilities()
{
this.API_KEY = "sl.BGf8pMsVDak2lPSTIaDlk58-IAO9KPfZlfxM2MbkW0aGPKexdXp51SmSG91GxiJMABxh5t56jlzXo69Yvsc8kAhmcIsuKrkj9XWxGQcaOqT8rzV2YEwesqKco45tJZIe_XE1mc91n8rY";
this.config = DbxRequestConfig.newBuilder("dropbox/java-tutorial").build();
this.client = new DbxClientV2(config, API_KEY);
}
public void downloadFile(String localPath, String dropboxPath) throws DbxException, ClassNotFoundException, IOException
{
if (dropboxPath.charAt(0) != '/') dropboxPath = "/" + dropboxPath;
FileOutputStream downloadedFile = new FileOutputStream(localPath);
try
{
client.files().download(dropboxPath).download(downloadedFile);
}
finally
{
downloadedFile.close();
}
}
public String getFileContent(String dropboxPath) throws DbxException, ClassNotFoundException, IOException
{
if (dropboxPath.charAt(0) != '/') dropboxPath = "/" + dropboxPath;
StringBuilder sb = new StringBuilder();
try (InputStream in = client.files().download(dropboxPath).getInputStream())
{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null)
{
sb.append(line);
sb.append("\n");
}
}
catch (Exception e)
{
return "";
}
return sb.toString();
}
public void uploadFile(String localPath, String dropboxPath) throws DbxException, ClassNotFoundException, IOException
{
if (dropboxPath.charAt(0) != '/') dropboxPath = "/" + dropboxPath;
FileInputStream uploadedFile = new FileInputStream(localPath);
try
{
client.files().uploadBuilder(dropboxPath).withMode(WriteMode.OVERWRITE).uploadAndFinish(uploadedFile);
}
finally
{
uploadedFile.close();
}
}
public ArrayList<String> listDropBoxFiles(String dropboxPath) throws DbxException, ClassNotFoundException, IOException
{
if (dropboxPath.equals("/")) dropboxPath = "";
ArrayList<String> fileList = new ArrayList<String>();
for (Metadata metadata : client.files().listFolder(dropboxPath).getEntries())
{
fileList.add(metadata.getPathLower());
}
return fileList;
}
public void createFolder(String dropboxPath) throws DbxException, ClassNotFoundException, IOException
{
if (dropboxPath.charAt(0) != '/') dropboxPath = "/" + dropboxPath;
client.files().createFolder(dropboxPath);
}
public ArrayList<String> uploadFolder(String localPath, String dropboxPath) throws DbxException, ClassNotFoundException, IOException
{
if (dropboxPath.charAt(0) != '/') dropboxPath = "/" + dropboxPath;
ArrayList<String> fileList = new ArrayList<String>();
Files.find(Paths.get(System.getProperty("user.dir") + "/" + localPath), 999, (p, bfa) -> true).forEach(path -> fileList.add(path.toString()));
ArrayList<String> outputString = new ArrayList<String>();
for (String file : fileList)
{
String fileName = "/" + file;
fileName = Paths.get(fileName).normalize().toString();
int absPathLength = (System.getProperty("user.dir") + "/").length();
fileName = dropboxPath + "/" + fileName.substring(absPathLength);
if (!fileName.contains(".coffee"))
System.out.println("Uploading " + fileName);
if (Files.isDirectory(Paths.get(file)))
{
try
{
createFolder(fileName);
}
catch (DbxException e)
{
;
}
}
else
{
uploadFile(file, fileName);
}
if (!fileName.startsWith(".coffee"))
{
outputString.add(fileName);
}
}
return outputString;
}
public LocalDateTime getLastDropBoxCommitTime(String dropboxPath) throws DbxException, ClassNotFoundException, IOException
{
if (dropboxPath.charAt(0) != '/') dropboxPath = "/" + dropboxPath;
dropboxPath = dropboxPath + "/.coffee";
String fileName, timestamp;
ArrayList<String> fileList = listDropBoxFiles(dropboxPath);
ArrayList<LocalDateTime> commitTimes = new ArrayList<LocalDateTime>();
for (String file : fileList)
{
fileName = file.substring(dropboxPath.length() + 1);
if (fileName.equals(".bean")) continue;
String[] fileNameSplit = fileName.split("--");
timestamp = fileNameSplit[fileNameSplit.length - 1];
LocalDateTime localDateTime = LocalDateTime.parse(timestamp, DateTimeFormatter.ofPattern("yyyy-MM-dd't'HH:mm:ss"));
commitTimes.add(localDateTime);
}
commitTimes.sort(Comparator.naturalOrder());
int num_commits = commitTimes.size();
if (num_commits == 0) return null;
return commitTimes.get(commitTimes.size() - 1);
}
public LocalDateTime getLastLocalCommitTime() throws DbxException, ClassNotFoundException, IOException
{
ArrayList<LocalDateTime> commitTimes = new ArrayList<LocalDateTime>();
File commitDirectory = new File(System.getProperty("user.dir") + "/.coffee");
String fileName, timestamp;
for (File file : commitDirectory.listFiles())
{
fileName = file.getName();
if (fileName.equals(".bean")) continue;
String[] fileNameSplit = fileName.split("--");
timestamp = fileNameSplit[fileNameSplit.length - 1];
LocalDateTime localDateTime = LocalDateTime.parse(timestamp, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
commitTimes.add(localDateTime);
}
commitTimes.sort(Comparator.naturalOrder());
int num_commits = commitTimes.size();
if (num_commits == 0) return null;
return commitTimes.get(commitTimes.size() - 1);
}
public void downloadFolder(String localPath, String dropboxPath, boolean pull) throws DbxException, ClassNotFoundException, IOException, InterruptedException
{
String absolutePath = System.getProperty("user.dir") + localPath;
if (pull == true)
{
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
}
localPath = absolutePath + "CLONE.zip";
localPath = Paths.get(localPath).normalize().toString();
if (dropboxPath.charAt(0) != '/') dropboxPath = "/" + dropboxPath;
FileOutputStream downloadedFolder = new FileOutputStream(localPath);
client.files().downloadZipBuilder(dropboxPath).start().download(downloadedFolder);
Process p = Runtime.getRuntime().exec("unzip -o " + localPath + " -d " + localPath.substring(0, localPath.length() - 9));
p.waitFor();
downloadedFolder.close();
File f = new File(localPath);
f.delete();
}
public void downloadFolder(String localPath, String dropboxPath) throws DbxException, ClassNotFoundException, IOException, InterruptedException
{
downloadFolder(localPath, dropboxPath, false);
}
}