yangys
8 天以前 f4c6e0e1308bccb943ca1cddfdf7f643b6b6a1aa
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
package org.springblade.mdm.machineback.controller;
import org.springblade.mdm.machineback.filewatch.DynamicDirectoryWatcher;
import org.springblade.mdm.machineback.filewatch.FileLockChecker;
import org.springblade.mdm.machineback.filewatch.FileWatcherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
 
@RestController
@RequestMapping("/machineback/file")
public class DirectoryWatcherController {
    private final DynamicDirectoryWatcher directoryWatcher;
 
    @Autowired
    public DirectoryWatcherController(DynamicDirectoryWatcher directoryWatcher) {
        this.directoryWatcher = directoryWatcher;
    }
 
    @PostMapping("/watch")
    public String watchDirectory(@RequestParam String directoryPath) {
        try {
            Path path = Paths.get(directoryPath);
            directoryWatcher.addDirectory(path, new SimpleFileChangeListener());
            return "Started watching directory: " + directoryPath;
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }
 
    @PostMapping("/unwatch")
    public String unwatchDirectory(@RequestParam String directoryPath) {
        try {
            Path path = Paths.get(directoryPath);
            directoryWatcher.removeDirectory(path);
            return "Stopped watching directory: " + directoryPath;
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }
    @PostMapping("/canlock")
    public String canlock(@RequestParam String filePath) {
        try {
            Path path = Paths.get(filePath);
            boolean comp = FileLockChecker.isFileCompletelyWritten(path);
 
            return filePath+ " lock: " + comp;
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }
    private static class SimpleFileChangeListener implements FileWatcherService.FileChangeListener {
        @Override
        public void onFileCreated(Path filePath) {
            System.out.println("File created: " + filePath);
        }
 
        @Override
        public void onFileModified(Path filePath) {
            System.out.println("File modified: " + filePath);
            //boolean comp = FileLockChecker.isFileCompletelyWritten(filePath);
            boolean comp = false;
            try {
                comp = FileLockChecker.isFileComplete(filePath);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            System.out.println("File FINISHED: " + comp);
        }
 
        @Override
        public void onFileDeleted(Path filePath) {
            System.out.println("File deleted: " + filePath);
        }
    }
}