⧠ãï½ããããã ãã®ãã¨ãã§ããã®ã«ãMECEï¼Mutually Exclusive, Collectively Exhaustiveï¼ã«åé¡ãããããä½æ¥ã¯ãã³ã³ãã ã£ããããã®ãããåãããã§ããª...
ã¨ãããããæ åç³»ã®è£½ä½ã¨ãã§PoCï¼Proof of Conceptï¼ãã§ããããã«ãªãã®ã¯è¯ãã§ããªã
ãã¡ã¤ã«ããã¼ã¸ã£ã¼ã¨ã¯
Wikipediaããã«ããã¾ãã¨ã
A file manager or file browser is a computer program that provides a user interface to manage files and folders. The most common operations performed on files or groups of files include creating, opening (e.g. viewing, playing, editing or printing), renaming, copying, moving, deleting and searching for files, as well as modifying file attributes, properties and file permissions. Folders and files may be displayed in a hierarchical tree based on their directory structure.
⧠ã¨ããã®ã ãã©ãå®ç¾©ãããªãææ§...
ãã¡ã¤ã«ããã©ã«ãã¼ã管çããæ©è½ãæä¾ããããã°ã©ã ã¨ãããã¨ã§ãä½ã管çã§ããã°è¯ããã¨ãç¹ã«æ±ºã¾ã£ã¦ãªãã£ã½ã...
ã¦ã£ãããOSï¼Operation Systemï¼ã®ãã¡ã¤ã«ã·ã¹ãã ã®ããã«ãOSï¼Operation Systemï¼ãæä¾ãã¦ããæ©è½ã®1ã¤ãã¨æã£ã¦ãããã ãã©ãããããããã§ã¯ç¡ãããã¨ãããã¨ãªãããã
ãã ããã¡ã¤ã«ã·ã¹ãã ã«ã¯ä¾åãã¦ããã¨æããã©ãã
ãã¡ã¤ã«ããã©ã«ãã«ã¢ã¯ã»ã¹ããæµãã¨ãã¦ã
⧠ä¸å³ã®ãããªä»çµã¿ã ã¨ããã¨ããã¡ã¤ã«ããã¼ã¸ã£ã¼ã¯ããSystem call interfaceããçµç±ããã¨ãããã¨ã«ãªãã¨ãããã¨ã§ãããã
Windowsã®ã¨ã¯ã¹ããã¼ã©ã¼ã®ããã«ãããã©ã«ãã§ç¨æããã¦ãããã¡ã¤ã«ããã¼ã¸ã£ã¼ã¨Linuxã®ãã¡ã¤ã«ããã¼ã¸ã£ã¼ã ã¨ææ³ãç°ãªããããªï¼
ãã¾ãã¡ãOSï¼Operation Systemï¼ã®æ©è½ã¨ãã¡ã¤ã«ããã¼ã¸ã£ã¼ã¨ã®é¢ä¿ãåããã...
ã¡ãªã¿ã«ã
⧠CUIã§ããã¡ã¤ã«ããã¼ã¸ã£ã¼ãå©ç¨ã§ãããããã®ã§ããã¡ã¤ã«ããã¼ã¸ã£ã¼ã®ä»çµã¿ãè¬ã§ããª...
ãã¡ã¤ã«ããã¼ã¸ã£ã¼ã®GUIã®ãããªãã¨ãçä¼¼ãã¦ãã©ã¦ã¶ã¼ä¸ã§ãã©ã«ãã®é層表示ãã¦ã¿ã
åã«ã
⧠WinMergeã¿ãããªãã¨ããã©ã¦ã¶ã§å®ç¾ãã¦ã¿ããã£ã¦è©±ããããã ãã©ãããã£ã½ããã®ã®ãããå°ã®ãããªãã®ãã§ããã®ã§ãã½ã¼ã¹ã³ã¼ããè¼ãã¦ããã¾ãã
ãããå°ã§ãã£ã¦ãç¡è¶è¦è¶ã«æéããã£ãããããã©ã¼ãã³ã¹ãæªãããJavaScriptãããåããããã¨ã«ãªã£ã¦ããã§ãå¾å´æãããªã...
ã¾ããä½ã¨ãªãã®é°å²æ°ã ãã§ãå ±æã§ããã°å¹¸ãã§ãã¨ã
â /test-web-file-system/build.gradle
plugins { id 'java' id 'org.springframework.boot' version '3.2.2' id 'io.spring.dependency-management' version '1.1.4' } group = 'com.example' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '21' } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { useJUnitPlatform() }
â /test-web-file-system/src/main/java/com/example/demo/config/CorsConfig.java
package com.example.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE"); } }
â /test-web-file-system/src/main/java/com/example/demo/dto/FileNodeDto.java
package com.example.demo.dto; import java.util.List; import lombok.Data; @Data public class FileNodeDto { private String name; private String type; // ãã¡ã¤ã«ã®ç¨®é¡ã示ã"type"ããããã£ã追å private long size; private long creationTime; private String fullPath; private String owner; private String group; private long lastModified; private PosixFilePermissionDto posixFilePermissionDto; private Listchildren; }
â /test-web-file-system/src/main/java/com/example/demo/dto/PosixFilePermissionDto.java
package com.example.demo.dto; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class PosixFilePermissionDto { private String filePermissionOfUser; private String filePermissionOfGroup; private String filePermissionOfOther; }
â /test-web-file-system/src/main/java/com/example/demo/dto/FolderStatisticsDto.java
package com.example.demo.dto; import lombok.Data; @Data public class FolderStatisticsDto { private int folderCount = 0; private int fileCount = 0; private long totalSize = 0; private long totalProcessTime = 0; }
â /test-web-file-system/src/main/java/com/example/demo/controller/FileTreeController.java
package com.example.demo.controller; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; import java.nio.file.attribute.PosixFileAttributeView; import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.dto.FileNodeDto; import com.example.demo.dto.FolderStatisticsDto; import com.example.demo.dto.PosixFilePermissionDto; @RestController public class FileTreeController { private final Map<String, FolderStatisticsDto> folderStatisticsMap = new HashMap<>(); private static int totalFolderCount = 0; private static int totalFileCount = 0; private static long totalSize = 0; private static long totalProcessTime = 0; @GetMapping("/filetree") public List<FileNodeDto> getFileTree() { String rootPath = "C:\\Users\\Toshinobu\\Desktop\\soft_work\\javascript_work"; File rootDirectory = new File(rootPath); List<FileNodeDto> fileNodeDto = buildFileTree(rootDirectory, rootDirectory.getAbsolutePath()); displayFolderStatistics(); displayTotalStatistics(); return fileNodeDto; } private List<FileNodeDto> buildFileTree(File directory, String parentPath) { final long startTime = System.nanoTime(); List<FileNodeDto> fileTree = new ArrayList<>(); File[] files = directory.listFiles(); BasicFileAttributes basicFileAttrs; long creationTime = 0; long lastModifiedTime = 0; if (files != null) { long folderSize = 0; for (File file : files) { try { basicFileAttrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); FileTime attrsCreatedTime = basicFileAttrs.creationTime(); FileTime attrsLastModifiedTime = basicFileAttrs.lastModifiedTime(); creationTime = attrsCreatedTime.toMillis(); lastModifiedTime = attrsLastModifiedTime.toMillis(); } catch (IOException e) { e.printStackTrace(); } FileNodeDto node = new FileNodeDto(); node.setName(file.getName()); // ãã¡ã¤ã«å long size = file.isDirectory() ? calculateFolderSize(file) : file.length(); node.setSize(size); totalSize += size; folderSize += size; node.setCreationTime(creationTime); // ä½ææ¥æ node.setLastModified(lastModifiedTime); // æçµæ´æ°æ¥æ node.setFullPath(parentPath + File.separator + file.getName()); // ãã©ã«ãã¼ããã¡ã¤ã«ã if (file.isDirectory()) { node.setType("folder"); node.setChildren(buildFileTree(file, node.getFullPath())); } else { node.setType("file"); } // ãã¼ããã·ã§ã³ node.setPosixFilePermissionDto(makePosixFilePermissionDto(file)); // é層æ§é ã«ãªã£ã¦ããå ´å fileTree.add(node); } updateFolderStatistics(parentPath, fileTree.size(), countFiles(fileTree), folderSize, System.nanoTime() - startTime); } return fileTree; } private PosixFilePermissionDto makePosixFilePermissionDto(File file) { final long startTime = System.nanoTime(); // PosixFileAttributeView ãåå¾ PosixFileAttributeView view = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class); PosixFilePermissionDto posixFilePermissionDto = new PosixFilePermissionDto(); if (view != null) { // PosixFileAttributes ãåå¾ PosixFileAttributes posixFileAttrs = null; try { posixFileAttrs = view.readAttributes(); } catch (IOException e) { // TODO èªåçæããã catch ããã㯠e.printStackTrace(); } // ãã¡ã¤ã«ã®ãã¼ããã·ã§ã³ãåå¾ Set<PosixFilePermission> posixFilePermissions = posixFileAttrs.permissions(); // ã¦ã¼ã¶ã¼ã®æ¨©é Set<PosixFilePermission> ownerPerms = PosixFilePermissions .fromString(PosixFilePermissions.toString(posixFilePermissions).substring(1, 4)); // ã°ã«ã¼ãã®æ¨©é Set<PosixFilePermission> groupPerms = PosixFilePermissions .fromString(PosixFilePermissions.toString(posixFilePermissions).substring(4, 7)); // ãã®ä»ã®ã¦ã¼ã¶ã¼ã®æ¨©é Set<PosixFilePermission> otherPerms = PosixFilePermissions .fromString(PosixFilePermissions.toString(posixFilePermissions).substring(7, 10)); // 権éã®æååè¡¨ç¾ posixFilePermissionDto.setFilePermissionOfUser(PosixFilePermissions.toString(ownerPerms)); posixFilePermissionDto.setFilePermissionOfGroup(PosixFilePermissions.toString(groupPerms)); posixFilePermissionDto.setFilePermissionOfOther(PosixFilePermissions.toString(otherPerms)); } else { // Windows ã·ã¹ãã ã§ããã°ããã¡ã¤ã«ã®æ¨©éæ å ±ãåå¾ã§ãã¾ãã System.out.println("This platform is not supported PosixFileAttributeView."); } final long endTime = System.nanoTime(); System.out.println("[start time][makePosixFilePermissionDto]" + TimeUnit.SECONDS.convert(startTime, TimeUnit.NANOSECONDS) + " sec."); System.out.println("[finish time][makePosixFilePermissionDto]" + TimeUnit.SECONDS.convert(endTime, TimeUnit.NANOSECONDS) + " sec."); System.out.println("[process time][makePosixFilePermissionDto]" + TimeUnit.SECONDS.convert((endTime - startTime), TimeUnit.NANOSECONDS) + " sec."); // System.out.println("[start time][makePosixFilePermissionDto]" + startTime / 1000000000.0 + " sec."); // System.out.println("[finish time][makePosixFilePermissionDto]" + endTime / 1000000000.0 + " sec."); // System.out.println("[process time][makePosixFilePermissionDto]" + (endTime - startTime) + " sec."); return posixFilePermissionDto; } // ãã©ã«ãçµ±è¨æ å ±ãæ´æ° private void updateFolderStatistics(String folderPath, int folderCount, int fileCount, long totalSize, long processTime) { FolderStatisticsDto statistics = folderStatisticsMap.getOrDefault(folderPath, new FolderStatisticsDto()); statistics.setFolderCount(statistics.getFolderCount() + folderCount); statistics.setFileCount(statistics.getFileCount() + fileCount); statistics.setTotalSize(statistics.getTotalSize() + totalSize); statistics.setTotalProcessTime(processTime); folderStatisticsMap.put(folderPath, statistics); // åè¨å¤æ´æ° totalFolderCount += folderCount; totalFileCount += fileCount; totalProcessTime += processTime; } // ãã©ã«ãçµ±è¨æ å ±ã表示 private void displayFolderStatistics() { System.out.println("Folder Statistics:"); for (Map.Entry<String, FolderStatisticsDto> entry : folderStatisticsMap.entrySet()) { String folderPath = entry.getKey(); FolderStatisticsDto statistics = entry.getValue(); System.out.println("Folder: " + folderPath); System.out.println(" - Folder Count: " + statistics.getFolderCount()); System.out.println(" - File Count: " + statistics.getFileCount()); System.out.println(" - Total Size: " + statistics.getTotalSize() + " bytes"); // System.out.println(" - Total Process Time (seconds): " + statistics.getTotalProcessTime() / 1_000_000_000.0); System.out.println(" - Total Process Time (seconds): " + TimeUnit.SECONDS.convert(statistics.getTotalProcessTime(), TimeUnit.NANOSECONDS)); } } // å ¨ä½ã®çµ±è¨æ å ±ã表示 private void displayTotalStatistics() { System.out.println("Total Statistics:"); System.out.println("Total Folder Count: " + totalFolderCount); System.out.println("Total File Count: " + totalFileCount); System.out.println("Total Size: " + totalSize + " bytes"); // System.out.println("Total Process Time (seconds): " + totalProcessTime / 1_000_000_000.0); System.out.println(" - Total Process Time (seconds): " + TimeUnit.SECONDS.convert(totalProcessTime, TimeUnit.NANOSECONDS)); } // ãã¡ã¤ã«æ°ãã«ã¦ã³ãããå帰é¢æ° private int countFiles(List<FileNodeDto> fileTree) { int count = 0; for (FileNodeDto node : fileTree) { if ("file".equals(node.getType())) { count++; } else if ("folder".equals(node.getType())) { count += countFiles(node.getChildren()); } } return count; } private long calculateFolderSize(File folder) { long size = 0; File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { size += calculateFolderSize(file); } else { size += file.length(); } } } return size; } }
ããã³ãã¨ã³ãå´
â C:\Users\Toshinobu\Desktop\soft_work\javascript_work\test-file-manager\.vscode\settings.json
{ "liveServer.settings.port": 5501 ,"[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode", }, "[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode", }, "[typescriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode", } , // èªåãã©ã¼ãããè¨å® "editor.formatOnSave": true, // ä¿åæã«ãã©ã¼ããã "editor.formatOnType": true, // å ¥åä¸ï¼æ¹è¡æï¼ã«ãã©ã¼ããã "editor.formatOnPaste": true, // ãã¼ã¹ãæã«ãã©ã¼ããã }
â C:\Users\Toshinobu\Desktop\soft_work\javascript_work\test-file-manager\index.html
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>ãã¡ã¤ã«ããã¼ã¸ã£ã¼</title> <style> #container { display: flex; height: 100vh; } #folderList { min-width: 300px; overflow-y: auto; border-right: 1px solid #ccc; } #selectList { width: 100%; } #fileList { flex: 2; overflow-y: auto; } .fileListTable { text-align: left; } .folder { list-style-type: none; } .folder > input { display: none; } .folder > label { cursor: pointer; display: flex; /* ãã§ãã¯ããã¯ã¹ã¨ããã¹ãã横並ã³ã«ãã */ align-items: center; /* åç´æ¹åã«ä¸å¤®æã */ } .folder > .folderContents { display: none; margin-left: 20px; } .folder > input:checked + label + .folderContents { display: block; } .file { list-style-type: none; margin-left: 20px; } /* ã«ã¹ã¿ã ã®ã³ã³ããã¹ãã¡ãã¥ã¼ã®ã¹ã¿ã¤ã« */ .context-menu { background-color: #f9f9f9; border: 1px solid #ccc; box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1); padding: 4px 0; position: absolute; z-index: 1000; } /* ã³ã³ããã¹ãã¡ãã¥ã¼ã®é ç®ã®ã¹ã¿ã¤ã« */ .context-menu div { padding: 8px 16px; cursor: pointer; } /* ã³ã³ããã¹ãã¡ãã¥ã¼ã®é ç®ã®ããã¼æã®ã¹ã¿ã¤ã« */ .context-menu div:hover { background-color: #ddd; } /* ã¢ã¼ãã«ã®ã¹ã¿ã¤ã« */ .modal { display: none; position: fixed; z-index: 1001; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0, 0, 0, 0.4); } /* ã¢ã¼ãã«ã®ã³ã³ãã³ãã®ã¹ã¿ã¤ã« */ .modal-content { background-color: #fefefe; margin: 10% auto; padding: 20px; border: 1px solid #888; width: 80%; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); } /* ã¢ã¼ãã«ã®ã¯ãã¼ãºãã¿ã³ã®ã¹ã¿ã¤ã« */ .close { color: #aaaaaa; float: right; font-size: 28px; font-weight: bold; } /* ã¢ã¼ãã«ã®ã¯ãã¼ãºãã¿ã³ã®ããã¼æã®ã¹ã¿ã¤ã« */ .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; } /* ã¢ã¼ãã«å ã®ãªã¹ãã®ã¹ã¿ã¤ã« */ .property-list { list-style-type: none; padding: 0; } .property-list li { margin-bottom: 10px; } .property-list li span { font-weight: bold; } #folderTree .file { margin-left: 0; /* æä¸ä½ã®ãã¡ã¤ã«ã«ãã¼ã¸ã³ãé©ç¨ããªã */ } .permissions-container { margin-left: 1rem; } .permission-matrix { display: flex; } .permission-label { min-width: 72px; } #folderPathValue { display: block; width: 100%; } </style> </head> <body> <div id="container"> <div id="folderList"> <ul id="folderTree"></ul> </div> <div id="selectList"> <div id="folderPath"> <input type="text" id="folderPathValue" value="" /> </div> <div id="fileList"></div> </div> </div> <!-- ããããã£ã¢ã¼ãã« --> <div id="propertyModal" class="modal"> <div class="modal-content"> <span class="close">Ã</span> <ul class="property-list" id="propertyContent"> <!-- ããããã£ã®å 容ãããã«è¡¨ç¤ºããã¾ãã --> </ul> </div> </div> <script> function getFolderFullPath(tree, folderName) { function getCommonPath(path1, path2) { const splitPath1 = path1.split(/[\\/]/); // ããã¯ã¹ã©ãã·ã¥ã¾ãã¯ã¹ã©ãã·ã¥ã§åå² const splitPath2 = path2.split(/[\\/]/); // ããã¯ã¹ã©ãã·ã¥ã¾ãã¯ã¹ã©ãã·ã¥ã§åå² let commonPath = ""; for ( let i = 0; i < Math.min(splitPath1.length, splitPath2.length); i++ ) { if (splitPath1[i] === splitPath2[i]) { commonPath += splitPath1[i] + "/"; } else { break; } } return commonPath; } function getRelativePath(fullPath, commonPath) { return fullPath.substring(commonPath.length); } let fullPath = ""; for (const item of tree) { if (item.name === folderName) { return item.fullPath; // ããããããã©ã«ããè¦ã¤ãã£ãããã®ãã«ãã¹ãè¿ã } if (item.children) { const childPath = getFolderFullPath(item.children, folderName); if (childPath) { const commonPath = getCommonPath(item.fullPath, childPath); const relativePath = getRelativePath(childPath, commonPath); const pathSeparator = item.fullPath.includes("\\") ? "\\" : "/"; return item.fullPath + pathSeparator + relativePath; } } } return fullPath; // ããããããã©ã«ããè¦ã¤ãããªãå ´åã¯ç©ºæååãè¿ã } // ãã©ã«ãã®ãã«ãã¹ã表示ããé¢æ° function updateFolderPath(folderPath) { const folderPathElement = document .getElementById("folderPath") .getElementsByTagName("input")[0]; if (folderPathElement) { folderPathElement.value = folderPath; } } let fileTreeData; // ãã¡ã¤ã«ããªã¼ãã¼ã¿ãæ ¼ç´ããå¤æ° function getFileTree() { fetch("http://localhost:8080/filetree") .then((response) => { if (!response.ok) { throw new Error("Network response was not ok"); } const contentType = response.headers.get("content-type"); if ( contentType && contentType.includes("application/octet-stream") ) { return response.blob(); // ãã¤ããªå½¢å¼ã®ãã¼ã¿ãåå¾ } else { return response.json(); // JSONå½¢å¼ã®ãã¡ã¤ã«ããªã¼ãã¼ã¿ãåå¾ } }) .then((data) => { if (typeof data === "string") { displayTextFileContents(data); // ããã¹ãå½¢å¼ã®ãã¼ã¿ã表示 } else { fileTreeData = data; displayFileTree(data, document.getElementById("folderTree")); } }) .then(() => { // å·¦ãµã¤ããã¼ã®ãã©ã«ããã¯ãªãã¯ãããæã®ã¤ãã³ããç»é² const folderList = document.querySelectorAll("li.folder"); for (var idx = 0; idx < folderList.length; idx++) { if (folderList[idx]) { folderList[idx].addEventListener("click", (event) => { console.dir(event); if (event.target && event.target.nodeName === "LABEL") { // const folderName = event.target.textContent; const folderFullPath = event.target.parentElement.dataset.fullPath; // const folderFullPath = getFolderFullPath( // fileTreeData, // folderName // ); // ãã©ã«ãã®ãã«ãã¹ãåå¾ renderFileList(folderFullPath); // ãã©ã«ãã®ãã«ãã¹ã渡ã updateFolderPath(folderFullPath); // å³ä¸ã«ãã©ã«ãã®ãã«ãã¹ã表示 } }); } } }) .catch((error) => console.error("Error fetching file tree:", error)); } // ãã¡ã¤ã«ããªã¼ã表示 function displayFileTree(fileTreeData, parentElement) { fileTreeData.forEach((node) => { if (node.type === "folder") { const li = document.createElement("li"); li.className = "folder"; const label = document.createElement("label"); const input = document.createElement("input"); input.type = "checkbox"; // å³ã¯ãªãã¯ã§ã³ã³ããã¹ãã¡ãã¥ã¼ã表示 label.addEventListener("contextmenu", function (event) { event.preventDefault(); // ããã©ã«ãã®ã³ã³ããã¹ãã¡ãã¥ã¼ããã£ã³ã»ã« showContextMenu(event.clientX, event.clientY, node); // ã«ã¹ã¿ã ã®ã³ã³ããã¹ãã¡ãã¥ã¼ã表示 }); label.appendChild(input); // inputãlabelã®åè¦ç´ ã¨ãã¦è¿½å label.appendChild(document.createTextNode(node.name)); // ããã¹ãã追å li.appendChild(label); // data å±æ§ã« fullPath ã®å¤ãè¨å® li.dataset.fullPath = node.fullPath; if (node.children) { const folderContents = document.createElement("ul"); folderContents.className = "folderContents"; // ã¯ã©ã¹åã追å folderContents.style.display = "none"; // 追å ï¼æåã¯é表示 displayFileTree(node.children, folderContents); li.appendChild(folderContents); // ãã¹ã¦ã®åè¦ç´ ã« fullPath ãè¨å®ãã // node.children.forEach((childNode) => { // const childLi = document.createElement("li"); // childLi.textContent = childNode.name; // childLi.dataset.fullPath = childNode.fullPath; // //folderContents.appendChild(childLi); // }); // ãã§ãã¯ããã¯ã¹ã®ç¶æ å¤åã«å¿ãã¦åè¦ç´ ã表示ã¾ãã¯é表示ã«ãã input.addEventListener("change", function () { folderContents.style.display = input.checked ? "block" : "none"; }); } parentElement.appendChild(li); } }); } // ããã¹ããã¡ã¤ã«ã®å 容ã表示 function displayTextFileContents(fileContent) { const textarea = document.createElement("textarea"); textarea.textContent = fileContent; // ãã¡ã¤ã«ã®å 容ãããã¹ãã¨ãªã¢ã«è¡¨ç¤º textarea.style.width = "100%"; textarea.style.height = "300px"; // é©åãªãµã¤ãºã«èª¿æ´ãã¦ãã ãã document.body.appendChild(textarea); } // ã«ã¹ã¿ã ã®ã³ã³ããã¹ãã¡ãã¥ã¼ã表示ããé¢æ° function showContextMenu(x, y, node) { // æ¢åã®ã³ã³ããã¹ãã¡ãã¥ã¼ãåé¤ const existingContextMenu = document.querySelector(".context-menu"); if (existingContextMenu) { existingContextMenu.remove(); } // ã¡ãã¥ã¼ã表示ããããã®HTMLè¦ç´ ãçæ const contextMenu = document.createElement("div"); contextMenu.className = "context-menu"; contextMenu.style.left = x + "px"; contextMenu.style.top = y + "px"; // æ°è¦ä½æãç·¨éããªãã¼ã ãåé¤ã¡ãã¥ã¼é ç®ã追å if (node.type === "folder") { addItem("æ°è¦ä½æ", function () { console.log("æ°è¦ä½æ"); }); addItem("ãªãã¼ã ", function () { console.log("ãªãã¼ã "); }); } else { addItem("ç·¨é", function () { console.log("ç·¨é"); }); } addItem("åé¤", function () { console.log("åé¤"); }); addItem("ããããã£", function () { showPropertyModal(node); }); // ããã¥ã¡ã³ãã«ã¡ãã¥ã¼ã追å document.body.appendChild(contextMenu); // ã³ã³ããã¹ãã¡ãã¥ã¼ãé表示ã«ããããã®ã¤ãã³ããªã¹ãã¼ã追å document.addEventListener( "click", function () { contextMenu.remove(); // ã¡ãã¥ã¼ãåé¤ }, { once: true } ); // ä¸åº¦ã ãå®è¡ãããããã«è¨å® // ã¡ãã¥ã¼é ç®ã追å ããé¢æ° function addItem(label, action) { const item = document.createElement("div"); item.textContent = label; item.addEventListener("click", action); contextMenu.appendChild(item); } } // ãã¼ããã·ã§ã³ãã¬ã³ããªã³ã°ããé¢æ° function renderPermissions(posixFilePermissionDto) { console.table(posixFilePermissionDto); if (!posixFilePermissionDto) return ""; // ãã¼ããã·ã§ã³æ å ±ããªãå ´åã¯ç©ºæååãè¿ã const permissionsArray = [ posixFilePermissionDto.filePermissionOfUser, posixFilePermissionDto.filePermissionOfGroup, posixFilePermissionDto.filePermissionOfOther, ]; const permissionLabels = ["èªã¿è¾¼ã¿", "æ¸ãè¾¼ã¿", "å®è¡"]; const permissionGroups = ["ã¦ã¼ã¶ã¼", "ã°ã«ã¼ã", "ãã®ä»"]; let permissionsHTML = ""; for (let i = 0; i < 3; i++) { permissionsHTML += `<div class="permission-matrix"><div class="permission-label"><strong>${permissionGroups[i]}</strong></div><div>: `; for (let j = 0; j < 3; j++) { const index = i * 3 + j; const checked = permissionsArray[i] && permissionsArray[i][j] === "1" ? "checked" : ""; permissionsHTML += `<label><input type="checkbox" ${checked} disabled> ${permissionLabels[j]}</label>`; } permissionsHTML += "</div></div>"; } console.log("permissionsHTML"); console.dir(permissionsHTML); return permissionsHTML; } // ããããã£ã®è©³ç´°æ å ±ã表示ããé¢æ° function showPropertyModal(node) { // ããããã£ã¢ã¼ãã«ã表示ããå¦çãå®è£ ãã const propertyModal = document.getElementById("propertyModal"); const propertyContent = document.getElementById("propertyContent"); propertyContent.innerHTML = ""; // æ¢åã®å 容ãã¯ãªã¢ const propertyItems = [ { label: "åå", value: node.name }, { label: "ãã«ãã¹", value: node.fullPath }, { label: "ãµã¤ãº", value: node.size }, { label: "ææã¦ã¼ã¶ã¼", value: node.owner }, { label: "ææã°ã«ã¼ã", value: node.group }, { label: "ãã¼ããã·ã§ã³", value: renderPermissions(node.posixFilePermissionDto), }, { label: "ä½ææ¥", value: formatDate(new Date(node.creationTime)) }, { label: "æ´æ°æ¥", value: formatDate(new Date(node.lastModified)) }, ]; propertyItems.forEach((item) => { const li = document.createElement("li"); const label = document.createElement("span"); label.textContent = item.label + ": "; const value = document.createTextNode(item.value); const valueContainer = document.createElement("span"); valueContainer.appendChild(value); li.appendChild(label); // ãã¼ããã·ã§ã³ã®å ´åã¯ç¹å¥ãªå¦çãè¡ã if (item.label === "ãã¼ããã·ã§ã³") { const permissionsContainer = document.createElement("div"); permissionsContainer.className = "permissions-container"; // ã¯ã©ã¹å±æ§ã追å permissionsContainer.innerHTML = item.value; // ãããä¿®æ£ãã li.appendChild(permissionsContainer); } else { li.appendChild(valueContainer); } propertyContent.appendChild(li); }); propertyModal.style.display = "block"; // ã¢ã¼ãã«ã®ã¯ãã¼ãºãã¿ã³ã®å¦çã追å const closeBtn = document.getElementsByClassName("close")[0]; closeBtn.onclick = function () { propertyModal.style.display = "none"; }; } // æ¥ä»ãæå®ãããå½¢å¼ã«ãã©ã¼ãããããé¢æ° function formatDate(date) { const year = date.getFullYear(); const month = ("0" + (date.getMonth() + 1)).slice(-2); const day = ("0" + date.getDate()).slice(-2); const hours = ("0" + date.getHours()).slice(-2); const minutes = ("0" + date.getMinutes()).slice(-2); const seconds = ("0" + date.getSeconds()).slice(-2); return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`; } // ãã«ãã¹ããNodeãåå¾ããé¢æ° function getNodeByPath(tree, path) { // ãã©ã«ãã®ãã«ãã¹ãããªãã¿ã§åå²ãã¦é åã«ãã const pathElements = path .split(/[\\/]/) .filter((element) => element !== ""); // 空ã®è¦ç´ ãé¤å¤ function findNodeInTree(nodes, pathElements) { for (const node of nodes) { const nodePathElements = node.fullPath .split(/[\\/]/) .filter((element) => element !== ""); // ãã©ã«ãåãå ¨ã¦ä¸è´ãããç¢ºèª if ( nodePathElements.length === pathElements.length && nodePathElements.every( (element, index) => element === pathElements[index] ) ) { return node; } // itemã«childrenãããããã®ä¸ã«ãã©ã«ãåãä¸è´ãããã®ããããç¢ºèª if (node.children) { const foundInChildren = findNodeInTree( node.children, pathElements ); if (foundInChildren) { return foundInChildren; } } } return null; } return findNodeInTree(tree, pathElements); } // å³å´ã®ãã©ã«ãã¨ãã¡ã¤ã«ã®ä¸è¦§ã表示ããé¢æ° function renderFileList(folderPath) { // æå®ããããã©ã«ãã®æ å ±ãåå¾ console.log(folderPath); const node = getNodeByPath(fileTreeData, folderPath); console.dir(node); if (node) { // ãã©ã«ãå ã®ãã¡ã¤ã«ã¨ãã©ã«ãã®ãªã¹ããåå¾ const folderContents = node.children; const fileListContainer = document.getElementById("fileList"); const fullPathContainer = document.getElementById("folderPath"); fileListContainer.innerHTML = ""; // ä¸è¦§ãã¯ãªã¢ // å³å´ã®ä¸é¨ã«ãã«ãã¹ã表示 fullPathContainer.textContent = folderPath; const table = document.createElement("table"); // ãã¼ãã«è¦ç´ ãä½æ table.classList.add("fileListTable"); // 追å ï¼ã¹ã¿ã¤ãªã³ã°ã®ããã®ã¯ã©ã¹ã追å // ãã¼ãã«ãããã¼ãä½æ const headerRow = document.createElement("tr"); const headers = [ "åå", "ææã¦ã¼ã¶ã¼", "ææã°ã«ã¼ã", "Type", "ä½ææ¥", "æ´æ°æ¥", ]; headers.forEach((headerText) => { const headerCell = document.createElement("th"); headerCell.textContent = headerText; headerRow.appendChild(headerCell); }); table.appendChild(headerRow); folderContents.forEach((item) => { const row = document.createElement("tr"); // æ°ããè¡ãä½æ const nameCell = document.createElement("td"); nameCell.textContent = item.name; row.appendChild(nameCell); const ownerCell = document.createElement("td"); ownerCell.textContent = item.owner; row.appendChild(ownerCell); const groupCell = document.createElement("td"); groupCell.textContent = item.group; row.appendChild(groupCell); const typeCell = document.createElement("td"); typeCell.textContent = item.type; row.appendChild(typeCell); const creationTimeCell = document.createElement("td"); creationTimeCell.textContent = formatDate( new Date(item.creationTime) ); row.appendChild(creationTimeCell); const lastModifiedCell = document.createElement("td"); lastModifiedCell.textContent = formatDate( new Date(item.lastModified) ); row.appendChild(lastModifiedCell); row.dataset.fullPath = item.fullPath; // item.fullPathãè¡ã«è¨å® row.addEventListener("contextmenu", (event) => { event.preventDefault(); // ããã©ã«ãã®ã³ã³ããã¹ãã¡ãã¥ã¼ããã£ã³ã»ã« showContextMenu(event.clientX, event.clientY, item); // ã«ã¹ã¿ã ã®ã³ã³ããã¹ãã¡ãã¥ã¼ã表示 }); // ãã©ã«ãã®å ´åã¯ã¯ãªãã¯ã¤ãã³ãã追å ãã¦ãã®ä¸èº«ã表示 if (item.type === "folder") { row.addEventListener("click", (event) => { const folderFullPath = event.target.parentElement.dataset.fullPath; // const folderFullPath = getFolderFullPath( // fileTreeData, // item.fullPath // ); console.log(folderFullPath); renderFileList(folderFullPath); // ãã©ã«ãã®ãã«ãã¹ã渡ãããã«ä¿®æ£ }); } table.appendChild(row); // ãã¼ãã«ã«è¡ã追å }); fileListContainer.appendChild(table); // ãã¼ãã«ã親è¦ç´ ã«è¿½å } else { console.log("Folder not found or is not a folder."); } } // åæ表示 getFileTree(); </script> </body> </html>
ã§ããµã¼ããµã¤ãå´ã®ã¢ããªã±ã¼ã·ã§ã³ã¯ãEclipseã§SpringBootããã¸ã§ã¯ããèµ·åã
ããã³ãã¨ã³ãå´ã®ã¢ããªã±ã¼ã·ã§ã³ã¯ãVisual Studio Codeã®æ¡å¼µæ©è½ã®ãLive Serverãã£ã¦ã®ã§èµ·åãã¦ã¾ãã
ããã³ãã¨ã³ãå´
ãµã¼ããµã¤ãå´
ãï½ãããããããWindowsã®ã¨ã¯ã¹ããã¼ã©ã¼ã®ä»çµã¿ãåãããã®ã§ãçä¼¼ãããããªããã ãã©...
ä»æ§ãåãããªãããé°å²æ°ã§ä½ã£ããã©ãè¨æ¸¬ã®å®è£ ãåã£ã¦ããã¨ããã¨ããã©ã«ãæ°ããã¡ã¤ã«æ°ãåããã¦ããã ã160000å¼±ã®æ°ãå¦çããã®ã«ã177ç§ã®æéãè¦ãã¦ããã®ã¯å³ããã§ããª...
ãï½ããè¨æ¸¬ãä¸æããã£ã¦ããªãã£ã½ããChatGPTã«ä»»ããã®ããºãã£ãããã§ããªã
å®éã«ã¯ã
⧠28000å¼±ã®æ°ãªãã ãã©ã52ç§ãããããã£ã¦ãã
ChatGPTãã¡ãã£ã¨é ·éãã...
çµå±ãèªåã§ä¿®æ£ãããã¨ã«ãªã£ãã®ã ãã©ãåè¨å¦çæéã¯ç´ããããªãã£ã...
â /test-web-file-system/src/main/java/com/example/demo/TestWebFileSystemApplication.java
package com.example.demo.controller; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; import java.nio.file.attribute.PosixFileAttributeView; import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.dto.FileNodeDto; import com.example.demo.dto.FolderStatisticsDto; import com.example.demo.dto.PosixFilePermissionDto; @RestController public class FileTreeController { private final Map<String, FolderStatisticsDto> folderStatisticsMap = new HashMap<>(); private static int totalFolderCount = 0; private static int totalFileCount = 0; private static long totalSize = 0; private static long totalProcessTime = 0; @GetMapping("/filetree") public List<FileNodeDto> getFileTree() { String rootPath = "C:\\Users\\Toshinobu\\Desktop\\soft_work\\javascript_work"; File rootDirectory = new File(rootPath); long startTime = TimeUnit.SECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS); List<FileNodeDto> fileNodeDto = buildFileTree(rootDirectory, rootDirectory.getAbsolutePath()); displayFolderStatistics(); displayTotalStatistics(); long finishTime = TimeUnit.SECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS); System.out.println(startTime); System.out.println(finishTime); System.out.println((finishTime - startTime)); return fileNodeDto; } private List<FileNodeDto> buildFileTree(File directory, String parentPath) { final long startTime = System.nanoTime(); List<FileNodeDto> fileTree = new ArrayList<>(); File[] files = directory.listFiles(); BasicFileAttributes basicFileAttrs; long creationTime = 0; long lastModifiedTime = 0; long totalSize = 0; long folderSize = 0; int folderCount = 0; int fileCount = 0; if (files != null) { for (File file : files) { try { basicFileAttrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); FileTime attrsCreatedTime = basicFileAttrs.creationTime(); FileTime attrsLastModifiedTime = basicFileAttrs.lastModifiedTime(); creationTime = attrsCreatedTime.toMillis(); lastModifiedTime = attrsLastModifiedTime.toMillis(); } catch (IOException e) { e.printStackTrace(); } FileNodeDto node = new FileNodeDto(); node.setName(file.getName()); // ãã¡ã¤ã«å long size = file.isDirectory() ? calculateFolderSize(file) : file.length(); node.setSize(size); // folderSize += size; totalSize += basicFileAttrs.size(); node.setCreationTime(creationTime); // ä½ææ¥æ node.setLastModified(lastModifiedTime); // æçµæ´æ°æ¥æ node.setFullPath(parentPath + File.separator + file.getName()); // ãã©ã«ãã¼ããã¡ã¤ã«ã if (file.isDirectory()) { node.setType("folder"); folderCount++; node.setChildren(buildFileTree(file, node.getFullPath())); } else { node.setType("file"); fileCount++; } // ãã¼ããã·ã§ã³ node.setPosixFilePermissionDto(makePosixFilePermissionDto(file)); // é層æ§é ã«ãªã£ã¦ããå ´å fileTree.add(node); } long processTime =System.nanoTime() - startTime; updateFolderStatistics(parentPath, folderCount, fileCount, folderSize, processTime); } return fileTree; } private PosixFilePermissionDto makePosixFilePermissionDto(File file) { final long startTime = System.nanoTime(); // PosixFileAttributeView ãåå¾ PosixFileAttributeView view = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class); PosixFilePermissionDto posixFilePermissionDto = new PosixFilePermissionDto(); if (view != null) { // PosixFileAttributes ãåå¾ PosixFileAttributes posixFileAttrs = null; try { posixFileAttrs = view.readAttributes(); } catch (IOException e) { // TODO èªåçæããã catch ããã㯠e.printStackTrace(); } // ãã¡ã¤ã«ã®ãã¼ããã·ã§ã³ãåå¾ Set<PosixFilePermission> posixFilePermissions = posixFileAttrs.permissions(); // ã¦ã¼ã¶ã¼ã®æ¨©é Set<PosixFilePermission> ownerPerms = PosixFilePermissions .fromString(PosixFilePermissions.toString(posixFilePermissions).substring(1, 4)); // ã°ã«ã¼ãã®æ¨©é Set<PosixFilePermission> groupPerms = PosixFilePermissions .fromString(PosixFilePermissions.toString(posixFilePermissions).substring(4, 7)); // ãã®ä»ã®ã¦ã¼ã¶ã¼ã®æ¨©é Set<PosixFilePermission> otherPerms = PosixFilePermissions .fromString(PosixFilePermissions.toString(posixFilePermissions).substring(7, 10)); // 権éã®æååè¡¨ç¾ posixFilePermissionDto.setFilePermissionOfUser(PosixFilePermissions.toString(ownerPerms)); posixFilePermissionDto.setFilePermissionOfGroup(PosixFilePermissions.toString(groupPerms)); posixFilePermissionDto.setFilePermissionOfOther(PosixFilePermissions.toString(otherPerms)); } else { // Windows ã·ã¹ãã ã§ããã°ããã¡ã¤ã«ã®æ¨©éæ å ±ãåå¾ã§ãã¾ãã System.out.println("This platform is not supported PosixFileAttributeView."); } final long endTime = System.nanoTime(); System.out.println("[start time][makePosixFilePermissionDto]" + TimeUnit.SECONDS.convert(startTime, TimeUnit.NANOSECONDS) + " sec."); System.out.println("[finish time][makePosixFilePermissionDto]" + TimeUnit.SECONDS.convert(endTime, TimeUnit.NANOSECONDS) + " sec."); System.out.println("[process time][makePosixFilePermissionDto]" + TimeUnit.SECONDS.convert((endTime - startTime), TimeUnit.NANOSECONDS) + " sec."); // System.out.println("[start time][makePosixFilePermissionDto]" + startTime / 1000000000.0 + " sec."); // System.out.println("[finish time][makePosixFilePermissionDto]" + endTime / 1000000000.0 + " sec."); // System.out.println("[process time][makePosixFilePermissionDto]" + (endTime - startTime) + " sec."); return posixFilePermissionDto; } // ãã©ã«ãçµ±è¨æ å ±ãæ´æ° private void updateFolderStatistics(String folderPath, int folderCount, int fileCount, long totalSize, long processTime) { FolderStatisticsDto statistics = folderStatisticsMap.getOrDefault(folderPath, new FolderStatisticsDto()); statistics.setFolderCount(statistics.getFolderCount() + folderCount); statistics.setFileCount(statistics.getFileCount() + fileCount); statistics.setTotalSize(statistics.getTotalSize() + totalSize); statistics.setTotalProcessTime(processTime); folderStatisticsMap.put(folderPath, statistics); // åè¨å¤æ´æ° totalFolderCount += folderCount; totalFileCount += fileCount; totalSize += folderSize; // åè¨ãµã¤ãºã«ãã©ã«ãã®ãµã¤ãºãå ç® totalProcessTime += processTime; } // ãã©ã«ãçµ±è¨æ å ±ã表示 private void displayFolderStatistics() { System.out.println("Folder Statistics:"); for (Map.Entry<String, FolderStatisticsDto> entry : folderStatisticsMap.entrySet()) { String folderPath = entry.getKey(); FolderStatisticsDto statistics = entry.getValue(); System.out.println("Folder: " + folderPath); System.out.println(" - Folder Count: " + statistics.getFolderCount()); System.out.println(" - File Count: " + statistics.getFileCount()); System.out.println(" - Total Size: " + statistics.getTotalSize() + " bytes"); // System.out.println(" - Total Process Time (seconds): " + statistics.getTotalProcessTime() / 1_000_000_000.0); System.out.println(" - Total Process Time (seconds): " + TimeUnit.SECONDS.convert(statistics.getTotalProcessTime(), TimeUnit.NANOSECONDS)); } } // å ¨ä½ã®çµ±è¨æ å ±ã表示 private void displayTotalStatistics() { System.out.println("Total Statistics:"); System.out.println("Total Folder Count: " + totalFolderCount); System.out.println("Total File Count: " + totalFileCount); System.out.println("Total Size: " + totalSize + " bytes"); // System.out.println("Total Process Time (seconds): " + totalProcessTime / 1_000_000_000.0); System.out.println(" - Total Process Time (seconds): " + TimeUnit.SECONDS.convert(totalProcessTime, TimeUnit.NANOSECONDS)); } // ãã¡ã¤ã«æ°ãã«ã¦ã³ãããå帰é¢æ° private int countFiles(List<FileNodeDto> fileTree) { int count = 0; for (FileNodeDto node : fileTree) { if ("file".equals(node.getType())) { count++; } else if ("folder".equals(node.getType())) { count += countFiles(node.getChildren()); } } return count; } private long calculateFolderSize(File folder) { long size = 0; File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { size += calculateFolderSize(file); } else { size += file.length(); } } } return size; } }
æéãåããããä¿®æ£ãé å¼µã£ã¦ã¿ã¾ããã...
2024å¹´2æ19æ¥ï¼æï¼è¿½è¨ï¼â ãããã
ãªãã»ã©ãå帰å¦çã§ããã©ã«ãã¼ã®å ¥ãåã®æ°ãç°ãªãã¨ãå¦çæéã§éè¤ãã¦ããé¨åããããããåè¨å¦çæéã«ã¤ãã¦ã¯ãæä¸ä½ã®ãã©ã«ãã¼ãå¦çãããæéã¨ãããã¨ã«ãªããããªã®ã§ãåç´ã«å帰å¦çæ¯ã®å¦çæéãåç®ããæ¹æ³ã§æ±ããã®ã¯ä¸å¯è½ã ã£ãã¨ãã話...
ãã£ã¦ããã¾ãããªãChatGPTãã...
⧠47ç§ã§ä¸è´ãã¦ããã®ã§æ°ä»ããã¨ã
æ³¥èãããæ¹ã ãã©ãå¦çã®ä»çµã¿ãç解ã§ããªãã£ãããã¹ãã¼ããªæ¹æ³ãæãã¤ããªãæã¯ãæç´ã«çªãé²ã¾ãããå¾ãªãæãããã¨ã
ã¾ããã¢ã«ã´ãªãºã ã«ç²¾éãã¦ããæèè ã«èãã°ç¬æ®ºã ã¨ã¯æããã¾ãã...
話ãå ã«æ»ãã¨ããªã®ã§ãå ¨ã¦ã®å¦çãå®äºããã¾ã§ã®å¦çæéã¯ãå帰å¦çã®å¤å´ã§æ±ããã®ãè¯ãã¨ãããã¨ã§ããªã
æçµçãªJavaã®ã½ã¼ã¹ã³ã¼ãã¯ã以ä¸ã§ãä»ã®ã½ã¼ã¹ã³ã¼ãã¯ãå¤æ´ãç¡ãã®ã§ãååã®ãã®ãå©ç¨ã§OKã
â /test-web-file-system/src/main/java/com/example/demo/dto/FolderStatisticsDto.java
package com.example.demo.dto; import java.time.LocalDateTime; import lombok.Data; @Data public class FolderStatisticsDto { private int folderCount = 0; private int fileCount = 0; private long totalSize = 0; private long totalProcessTime = 0; private LocalDateTime startDateTime; private LocalDateTime finishDateTime; }
â /test-web-file-system/src/main/java/com/example/demo/controller/FileTreeController.java
package com.example.demo.controller; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; import java.nio.file.attribute.PosixFileAttributeView; import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.dto.FileNodeDto; import com.example.demo.dto.FolderStatisticsDto; import com.example.demo.dto.PosixFilePermissionDto; @RestController public class FileTreeController { private final Map<String, FolderStatisticsDto> folderStatisticsMap = new HashMap<>(); private static int totalFolderCount = 0; private static int totalFileCount = 0; private static long totalSize = 0; private static long totalProcessTime = 0; @GetMapping("/filetree") public List<FileNodeDto> getFileTree() { // 対象ã®ãã£ã¬ã¯ã㪠String rootPath = "C:\\Users\\Toshinobu\\Desktop\\soft_work\\javascript_work"; File rootDirectory = new File(rootPath); // å¦çéå§æé LocalDateTime startDateTime = LocalDateTime.now(); // ããã³ãã¨ã³ãå´ã«è¿ãæ å ±ãä½æãã List<FileNodeDto> fileNodeDto = buildFileTree(rootDirectory, rootDirectory.getAbsolutePath()); // å¦ççµäºæé LocalDateTime finishDateTime = LocalDateTime.now(); // å¦çæéã®ç®åº Instant instantStartTime = startDateTime.atZone(ZoneId.systemDefault()).toInstant(); Instant instantFinishTime = finishDateTime.atZone(ZoneId.systemDefault()).toInstant(); long elapsedTime = TimeUnit.NANOSECONDS.convert(instantFinishTime.getEpochSecond(), TimeUnit.SECONDS) - TimeUnit.NANOSECONDS.convert(instantStartTime.getEpochSecond(), TimeUnit.SECONDS); // çµ±è¨ displayFolderStatistics(); displayTotalStatistics(); // å¦çéå§æ¥æãåºå System.out.println("Start time" + startDateTime); // å¦ççµäºæ¥æãåºå System.out.println("Finish time" + finishDateTime); // å¦çæéï¼ç§ï¼ãåºå System.out.println("Total time: " + TimeUnit.SECONDS.convert(elapsedTime, TimeUnit.NANOSECONDS) + " seconds"); // ããã³ãã¨ã³ãå´ã«ã¬ã¹ãã³ã¹ãè¿ã return fileNodeDto; } private List<FileNodeDto> buildFileTree(File directory, String parentPath) { LocalDateTime startDateTime = LocalDateTime.now(); // å¦çéå§æ¥æ LocalDateTime finishDateTime = null; // å¦ççµäºæ¥æ long processTime = 0; // å¦çæé List<FileNodeDto> fileTree = new ArrayList<>(); // ãã£ã¬ã¯ããªã«å«ã¾ãããã¡ã¤ã«ãã¾ãã¯ããã©ã«ãã®ä¸è¦§ File[] files = directory.listFiles(); BasicFileAttributes basicFileAttrs; int folderCount = 0; // ãã©ã«ãã¼æ°ã«ã¦ã³ãç¨ int fileCount = 0; // ãã¡ã¤ã«æ°ã«ã¦ã³ãç¨ long totalSize = 0; // åè¨ãµã¤ãº if (files != null) { for (File file : files) { try { // ãã¡ã¤ã«ã®ä½ææ¥ãæ´æ°æ¥ã®æ å ±ãåå¾ basicFileAttrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class); FileTime attrsCreatedTime = basicFileAttrs.creationTime(); FileTime attrsLastModifiedTime = basicFileAttrs.lastModifiedTime(); long creationTime = attrsCreatedTime.toMillis(); long lastModifiedTime = attrsLastModifiedTime.toMillis(); // ããã³ãã¨ã³ãå´ã«è¿ãæ å ±ãä½æãã FileNodeDto node = new FileNodeDto(); // ãã¡ã¤ã«åãã¾ãã¯ããã©ã«ãåãè¨å® node.setName(file.getName()); // ãµã¤ãºãè¨å® long size = file.isDirectory() ? calculateFolderSize(file) : file.length(); node.setSize(size); // folderSize += size; // ç·åè¨ã®ãµã¤ãº totalSize += basicFileAttrs.size(); // ä½ææ¥ãè¨å® node.setCreationTime(creationTime); // æ´æ°æ¥ãè¨å® node.setLastModified(lastModifiedTime); // ãã«ãã¹ãè¨å® node.setFullPath(parentPath + File.separator + file.getName()); // ãã©ã«ãã®å ´å if (file.isDirectory()) { node.setType("folder"); folderCount++; node.setChildren(buildFileTree(file, node.getFullPath())); // ãã¡ã¤ã«ã®å ´å } else { node.setType("file"); fileCount++; } // å¦çæéãç®åº finishDateTime = LocalDateTime.now(); Instant instantStartTime = startDateTime.atZone(ZoneId.systemDefault()).toInstant(); Instant instantFinishTime = finishDateTime.atZone(ZoneId.systemDefault()).toInstant(); processTime = TimeUnit.NANOSECONDS.convert(instantFinishTime.getEpochSecond(), TimeUnit.SECONDS) - TimeUnit.NANOSECONDS.convert(instantStartTime.getEpochSecond(), TimeUnit.SECONDS); // ãã¡ã¤ã«æ¨©éãè¨å® node.setPosixFilePermissionDto(makePosixFilePermissionDto(file)); // ãªã¹ãã«è¿½å fileTree.add(node); } catch (IOException e) { e.printStackTrace(); } } } // çµ±è¨ãæ´æ° updateFolderStatistics(parentPath, folderCount, fileCount, totalSize, processTime, startDateTime, finishDateTime); return fileTree; } private PosixFilePermissionDto makePosixFilePermissionDto(File file) { // PosixFileAttributeView ãåå¾ PosixFileAttributeView view = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class); PosixFilePermissionDto posixFilePermissionDto = new PosixFilePermissionDto(); if (view != null) { // PosixFileAttributes ãåå¾ try { PosixFileAttributes posixFileAttrs = view.readAttributes(); // ãã¡ã¤ã«ã®ãã¼ããã·ã§ã³ãåå¾ Set<PosixFilePermission> posixFilePermissions = posixFileAttrs.permissions(); // ã¦ã¼ã¶ã¼ã®æ¨©é Set<PosixFilePermission> ownerPerms = PosixFilePermissions .fromString(PosixFilePermissions.toString(posixFilePermissions).substring(1, 4)); // ã°ã«ã¼ãã®æ¨©é Set<PosixFilePermission> groupPerms = PosixFilePermissions .fromString(PosixFilePermissions.toString(posixFilePermissions).substring(4, 7)); // ãã®ä»ã®ã¦ã¼ã¶ã¼ã®æ¨©é Set<PosixFilePermission> otherPerms = PosixFilePermissions .fromString(PosixFilePermissions.toString(posixFilePermissions).substring(7, 10)); // 権éã®æååè¡¨ç¾ posixFilePermissionDto.setFilePermissionOfUser(PosixFilePermissions.toString(ownerPerms)); posixFilePermissionDto.setFilePermissionOfGroup(PosixFilePermissions.toString(groupPerms)); posixFilePermissionDto.setFilePermissionOfOther(PosixFilePermissions.toString(otherPerms)); } catch (IOException e) { e.printStackTrace(); } } else { // Windows ã·ã¹ãã ã§ããã°ããã¡ã¤ã«ã®æ¨©éæ å ±ãåå¾ã§ãã¾ãã System.out.println("This platform is not supported PosixFileAttributeView."); } return posixFilePermissionDto; } // ãã©ã«ãçµ±è¨æ å ±ãæ´æ° private void updateFolderStatistics(String folderPath, int folderCount, int fileCount, long folderSize, long processTime, LocalDateTime startDateTime, LocalDateTime finsihDateTime) { FolderStatisticsDto statistics = folderStatisticsMap.getOrDefault(folderPath, new FolderStatisticsDto()); statistics.setFolderCount(statistics.getFolderCount() + folderCount); statistics.setFileCount(statistics.getFileCount() + fileCount); statistics.setTotalSize(statistics.getTotalSize() + folderSize); // åè¨ãµã¤ãºã«ãã©ã«ãã®ãµã¤ãºãå ç® statistics.setTotalProcessTime(statistics.getTotalProcessTime() + processTime); statistics.setStartDateTime(startDateTime); statistics.setFinishDateTime(finsihDateTime); folderStatisticsMap.put(folderPath, statistics); totalFolderCount += folderCount; totalFileCount += fileCount; totalSize += folderSize; // åè¨ãµã¤ãºã«ãã©ã«ãã®ãµã¤ãºãå ç® totalProcessTime += processTime; } // ãã©ã«ãçµ±è¨æ å ±ã表示 private void displayFolderStatistics() { System.out.println("Folder Statistics:"); for (Map.Entry<String, FolderStatisticsDto> entry : folderStatisticsMap.entrySet()) { String folderPath = entry.getKey(); FolderStatisticsDto statistics = entry.getValue(); System.out.println("Folder: " + folderPath); System.out.println(" - Folder Count: " + statistics.getFolderCount()); System.out.println(" - File Count: " + statistics.getFileCount()); System.out.println(" - Total Size: " + statistics.getTotalSize() + " bytes"); System.out.println(" - StarteDateTime: " + statistics.getStartDateTime()); System.out.println(" - FinishDateTime: " + statistics.getFinishDateTime()); System.out.println(" - Total Process Time (seconds): " + TimeUnit.SECONDS.convert(statistics.getTotalProcessTime(), TimeUnit.NANOSECONDS)); } } private void displayTotalStatistics() { System.out.println("Total Statistics:"); System.out.println("Total Folder Count: " + totalFolderCount); System.out.println("Total File Count: " + totalFileCount); System.out.println("Total Size: " + totalSize + " bytes"); System.out.println("Total Process Time (seconds): " + TimeUnit.SECONDS.convert(totalProcessTime, TimeUnit.NANOSECONDS)); } private long calculateFolderSize(File folder) { long size = 0; File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { size += calculateFolderSize(file); } else { size += file.length(); } } } return size; } }
⧠ã³ã³ããã¼ã©ã¼ã®å¦çã¯ãããã³ãã¨ã³ãå´ãããªã¯ã¨ã¹ãåãåãã¡ã½ãã以å¤ã¯ããµã¼ãã¹ã¯ã©ã¹ã¨ãã«å¤åºãããæ¹ãè¯ãã¨æããã ãã©ãä»åã¯å²æã
2024å¹´2æ19æ¥ï¼æï¼è¿½è¨ï¼â ããã¾ã§
å¦çæéç縮ã«ããã¼ã¸ã³ã°ã¨ãã使ãæ¹ãè¯ããã ãããï¼
ããã ã¨ãããã³ãã¨ã³ãã¨ãµã¼ããµã¤ãã®éä¿¡ã®é »åº¦ãå¤ãã¯ãªãã¨æããã ãã©ãæ£è§£ãåããã...
æ¯åº¦ã¢ã¤ã¢ã¤æãå端ãªã...
ä»åã¯ãã®ã¸ãã§ã
Â
Â