-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from umjammer/0.1.6
0.1.6
- Loading branch information
Showing
22 changed files
with
413 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 48 additions & 130 deletions
178
vavi-nio-file-onedrive/src/test/java/vavi/nio/file/onedrive/Main5.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,81 @@ | ||
/* | ||
* Copyright (c) 2017 by Naohide Sano, All rights reserved. | ||
* Copyright (c) 2020 by Naohide Sano, All rights reserved. | ||
* | ||
* Programmed by Naohide Sano | ||
*/ | ||
|
||
package vavi.nio.file.onedrive; | ||
|
||
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.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
|
||
import vavi.net.auth.oauth2.OAuth2AppCredential; | ||
import vavi.net.auth.oauth2.microsoft.MicrosoftLocalAppCredential; | ||
import vavi.util.properties.annotation.PropsEntity; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static java.nio.file.FileVisitResult.CONTINUE; | ||
import vavi.nio.file.Base; | ||
import vavi.nio.file.Util; | ||
import vavi.util.Debug; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
|
||
/** | ||
* onedrive renamer | ||
* OneDrive. (OneDriveJavaSDK engine) | ||
* | ||
* @author <a href="mailto:[email protected]">Naohide Sano</a> (umjammer) | ||
* @version 0.00 2017/03/14 umjammer initial version <br> | ||
* @version 0.00 2020/05/31 umjammer initial version <br> | ||
*/ | ||
public final class Main5 { | ||
public class Main5 { | ||
|
||
public static void main(final String... args) throws IOException { | ||
String email = args[0]; | ||
@Test | ||
void test01() throws Exception { | ||
String email = System.getenv("TEST5_ACCOUNT"); | ||
|
||
// Create the necessary elements to create a filesystem. | ||
// Note: the URI _must_ have a scheme of "onedrive", and | ||
// _must_ be hierarchical. | ||
URI uri = URI.create("onedrive:///?id=" + email); | ||
|
||
OAuth2AppCredential appCredential = new MicrosoftLocalAppCredential(); | ||
PropsEntity.Util.bind(appCredential); | ||
|
||
Map<String, Object> env = new HashMap<>(); | ||
env.put(OneDriveFileSystemProvider.ENV_APP_CREDENTIAL, appCredential); | ||
|
||
try (FileSystem onedrivefs = new OneDriveFileSystemProvider().newFileSystem(uri, env)) { | ||
|
||
Path root = onedrivefs.getRootDirectories().iterator().next(); | ||
FileRenamer.Replacer replacer = new RegexReplacer("\\ \\ ", " "); | ||
FileRenamer fileRenamer = new FileRenamer(replacer); | ||
Files.walkFileTree(root, fileRenamer); | ||
fileRenamer.exec(true); | ||
} | ||
|
||
System.exit(0); | ||
} | ||
|
||
static class RegexReplacer implements FileRenamer.Replacer { | ||
|
||
String regex; | ||
String replacement; | ||
|
||
Pattern pattern; | ||
|
||
RegexReplacer(String regex, String replacement) { | ||
this.regex = regex; | ||
this.replacement = replacement; | ||
pattern = Pattern.compile(regex); | ||
} | ||
|
||
@Override | ||
public boolean find(String source) { | ||
return pattern.matcher(source).find(); | ||
} | ||
|
||
@Override | ||
public String replace(String source) { | ||
return source.replaceAll(regex, replacement); | ||
} | ||
} | ||
|
||
static class FileRenamer extends SimpleFileVisitor<Path> { | ||
|
||
interface Replacer { | ||
boolean find(String source); | ||
String replace(String source); | ||
} | ||
Path src; | ||
Path dstDir; | ||
Path dst; | ||
String a, b; | ||
try (FileSystem onedrivefs = new OneDriveFileSystemProvider().newFileSystem(uri, Collections.EMPTY_MAP)) { | ||
|
||
Replacer replacer; | ||
src = Paths.get("src/test/resources/Hello.java"); | ||
dstDir = onedrivefs.getPath("/").resolve("TEST_FUSE_5"); | ||
dst = dstDir.resolve("テスト 001"); | ||
|
||
FileRenamer(Replacer replacer) { | ||
this.replacer = replacer; | ||
} | ||
|
||
class Pair { | ||
Pair(Path source, Path target) { | ||
this.source = source; | ||
this.target = target; | ||
if (Files.exists(dstDir)) { | ||
Base.removeTree(dstDir); | ||
} | ||
Path source; | ||
Path target; | ||
System.out.println("$ mkdir " + dstDir); | ||
Files.createDirectory(dstDir); | ||
|
||
System.out.println("$ cp " + src + " " + dst); | ||
Files.copy(src, dst); // TODO w/o dstDir it's works | ||
System.out.println("$ ls " + dstDir); | ||
Files.list(dstDir).forEach(System.err::println); | ||
a = Util.toFilenameString(Files.list(dstDir).findFirst().get()); | ||
} | ||
|
||
List<Pair> list = new ArrayList<>(); | ||
|
||
// Print information about | ||
// each type of file. | ||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) { | ||
if (attr.isSymbolicLink()) { | ||
System.err.format("Symbolic link: %s ", file); | ||
} else if (attr.isRegularFile()) { | ||
String name = file.getFileName().toString(); | ||
String newName = replacer.replace(name); | ||
if (replacer.find(name)) { | ||
System.out.format("mv '%s' '%s'\n", name, newName); | ||
list.add(new Pair(file, file.resolveSibling(newName))); | ||
} | ||
} else { | ||
System.err.format("Other : %s ", file); | ||
} | ||
return CONTINUE; | ||
} | ||
try (FileSystem onedrivefs = new OneDriveFileSystemProvider().newFileSystem(uri, Collections.EMPTY_MAP)) { | ||
dstDir = onedrivefs.getPath("/").resolve("TEST_FUSE_5"); | ||
dst = dstDir.resolve("テスト 001"); | ||
|
||
// Print each directory visited. | ||
@Override | ||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) { | ||
System.err.format("Directory : %s%n", dir); | ||
return CONTINUE; | ||
} | ||
System.out.println("$ ls " + dstDir); | ||
Files.list(dstDir).forEach(System.out::println); | ||
b = Util.toFilenameString(Files.list(dstDir).findFirst().get()); | ||
|
||
// If there is some error accessing | ||
// the file, let the user know. | ||
// If you don't override this method | ||
// and an error occurs, an IOException | ||
// is thrown. | ||
@Override | ||
public FileVisitResult visitFileFailed(Path file, IOException exc) { | ||
System.err.println(exc); | ||
return CONTINUE; | ||
} | ||
assertTrue(Files.exists(dst)); | ||
Debug.println(a + ", " + b); | ||
assertEquals(a, b); | ||
|
||
public void exec(boolean isDryRun) { | ||
list.forEach(pair -> { | ||
try { | ||
if (!isDryRun) { | ||
Files.move(pair.source, pair.target); | ||
} | ||
System.err.print("."); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
System.err.println("\nDone"); | ||
System.out.println("$ rm " + dst); | ||
Files.delete(dst); | ||
System.out.println("$ rmdir " + dstDir); | ||
Files.delete(dstDir); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.