Skip to content

Commit

Permalink
Merge pull request #11 from umjammer/0.1.6
Browse files Browse the repository at this point in the history
0.1.6
  • Loading branch information
umjammer authored Jun 25, 2020
2 parents d844454 + b0900ea commit ade22ce
Show file tree
Hide file tree
Showing 22 changed files with 413 additions and 192 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>vavi</groupId>
<artifactId>vavi-apps-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>

<properties>
<vavi-net-auth.version>0.1.2</vavi-net-auth.version>
Expand Down Expand Up @@ -156,7 +156,7 @@ TODO
<dependency>
<groupId>vavi</groupId>
<artifactId>vavi-net-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -195,13 +195,13 @@ TODO
<dependency>
<groupId>vavi</groupId>
<artifactId>vavi-net-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>vavi</groupId>
<artifactId>vavi-net-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Expand Down
4 changes: 2 additions & 2 deletions vavi-net-fuse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>vavi</groupId>
<artifactId>vavi-apps-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
</parent>

<artifactId>vavi-net-fuse</artifactId>
Expand Down Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupId>com.github.umjammer</groupId>
<artifactId>vavi-commons</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion vavi-nio-file-amazondrive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>vavi</groupId>
<artifactId>vavi-apps-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
</parent>

<artifactId>vavi-nio-file-amazondrive</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion vavi-nio-file-archive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>vavi</groupId>
<artifactId>vavi-apps-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
</parent>

<artifactId>vavi-nio-file-archive</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion vavi-nio-file-flickr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>vavi</groupId>
<artifactId>vavi-apps-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
</parent>

<artifactId>vavi-nio-file-flickr</artifactId>
Expand Down
6 changes: 3 additions & 3 deletions vavi-nio-file-gathered/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>vavi</groupId>
<artifactId>vavi-apps-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
</parent>

<artifactId>vavi-nio-file-gathered</artifactId>
Expand Down Expand Up @@ -89,13 +89,13 @@
<dependency>
<groupId>vavi</groupId>
<artifactId>vavi-nio-file-googledrive</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>vavi</groupId>
<artifactId>vavi-nio-file-onedrive4</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion vavi-nio-file-googledrive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>vavi</groupId>
<artifactId>vavi-apps-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
</parent>

<artifactId>vavi-nio-file-googledrive</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion vavi-nio-file-hfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>vavi</groupId>
<artifactId>vavi-apps-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
</parent>

<artifactId>vavi-nio-file-hfs</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion vavi-nio-file-onedrive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>vavi</groupId>
<artifactId>vavi-apps-fuse</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
</parent>

<artifactId>vavi-nio-file-onedrive</artifactId>
Expand Down
178 changes: 48 additions & 130 deletions vavi-nio-file-onedrive/src/test/java/vavi/nio/file/onedrive/Main5.java
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);
}
}
}
}
Loading

0 comments on commit ade22ce

Please sign in to comment.