-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain7.java
149 lines (117 loc) · 4.54 KB
/
Main7.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
/*
* Copyright (c) 2021 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/
package vavi.nio.file.googledrive;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import vavi.nio.file.googledrive.GoogleDriveUserDefinedFileAttributesProvider.RevisionsUtil;
import vavi.util.Debug;
import static java.nio.file.FileVisitResult.CONTINUE;
/**
* GoogleDrive user:revision.
*
* @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (umjammer)
* @version 0.00 2021/10/30 umjammer initial version <br>
*/
public class Main7 {
@Test
@Disabled
void test01() throws Exception {
String email = System.getenv("GOOGLE_TEST_ACCOUNT");
URI uri = URI.create("googledrive:///?id=" + email);
FileSystem fs = new GoogleDriveFileSystemProvider().newFileSystem(uri, Collections.emptyMap());
// ...
fs.close();
}
//
public static void main(String[] args) throws IOException {
t1(args);
}
/** list */
static void t2(String[] args) throws IOException {
String email = System.getenv("GOOGLE_TEST_ACCOUNT");
URI uri = URI.create("googledrive:///?id=" + email);
FileSystem fs = new GoogleDriveFileSystemProvider().newFileSystem(uri, Collections.emptyMap());
Files.list(fs.getPath("/tmp")).forEach(p -> {
try {
if (!Files.isDirectory(p)) {
System.err.println(p + "\n" + new String((byte[]) Files.getAttribute(p, "user:revisions")));
}
} catch (IOException e) {
System.err.println(e);
}
});
fs.close();
}
/** remove all remaining latest */
static void t3(String[] args) throws IOException {
String email = System.getenv("GOOGLE_TEST_ACCOUNT");
URI uri = URI.create("googledrive:///?id=" + email);
FileSystem fs = new GoogleDriveFileSystemProvider().newFileSystem(uri, Collections.emptyMap());
Path path = fs.getPath("/tmp/heroku-blog-duke.png");
System.err.println("--- before ---");
byte[] in = (byte[]) Files.getAttribute(path, "user:revisions");
byte[] out = RevisionsUtil.getLatestOnly(in);
System.err.println("--- write ---");
System.err.println(new String(out));
Files.setAttribute(path, "user:revisions", out);
System.err.println("--- after ---");
in = (byte[]) Files.getAttribute(path, "user:revisions");
RevisionsUtil.getLatestOnly(in);
fs.close();
}
static class FileSearcher extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
if (attr.isRegularFile()) {
try {
int size = RevisionsUtil.size(Files.getAttribute(file, "user:revisions"));
if (size > 1) {
System.err.println(file + ": " + size);
System.err.println("--- before ---");
byte[] in = (byte[]) Files.getAttribute(file, "user:revisions");
byte[] out = RevisionsUtil.getLatestOnly(in);
System.err.println("--- write ---");
System.err.println(new String(out));
Files.setAttribute(file, "user:revisions", out);
System.err.println("--- after ---");
in = (byte[]) Files.getAttribute(file, "user:revisions");
RevisionsUtil.getLatestOnly(in);
}
} catch (IOException e) {
System.err.println(e);
}
}
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.err.println(exc);
return CONTINUE;
}
}
/** remove all remaining latest, tree */
static void t1(String[] args) throws IOException {
String email = System.getenv("GOOGLE_TEST_ACCOUNT");
URI uri = URI.create("googledrive:///?id=" + email);
FileSystem fs = new GoogleDriveFileSystemProvider().newFileSystem(uri, Collections.emptyMap());
Files.walkFileTree(fs.getPath("/Books/Calibre Library"), new FileSearcher());
fs.close();
Debug.println("Done");
}
}