commit aa93d2d6d639739123e098c3767d281cbad5b296 Author: dai_48k <1981669259@qq.com> Date: Mon Mar 17 15:12:01 2025 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d803c5f --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +config.properties +.mvn +.idea +target +directory.db +webdav.iml \ No newline at end of file diff --git a/config-demo.properties b/config-demo.properties new file mode 100644 index 0000000..a5f9cb8 --- /dev/null +++ b/config-demo.properties @@ -0,0 +1,5 @@ +url=https://xxx.xxx/dav/ +account=xxxxx@qq.com +password=xxxxxxx +homePath=E:\\data\\test +root=test \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e66af37 --- /dev/null +++ b/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + top.dreamcenter + webdav + 1.0-SNAPSHOT + + webdav + https://www.dreamcenter.top + + + UTF-8 + 8 + + + + + + org.junit + junit-bom + 5.11.0 + pom + import + + + + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-params + test + + + org.apache.httpcomponents + httpclient + 4.5.13 + + + + org.xerial + sqlite-jdbc + 3.36.0.3 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.6.0 + + + package + + shade + + + false + + + top.dreamcenter.webdav.Main + + + + + + + + + diff --git a/src/main/java/top/dreamcenter/webdav/Main.java b/src/main/java/top/dreamcenter/webdav/Main.java new file mode 100644 index 0000000..620ae76 --- /dev/null +++ b/src/main/java/top/dreamcenter/webdav/Main.java @@ -0,0 +1,45 @@ +package top.dreamcenter.webdav; + +import top.dreamcenter.webdav.prop.WebDavConfig; +import top.dreamcenter.webdav.util.ProjectUtil; +import top.dreamcenter.webdav.util.WebDavUtil; + +import java.io.File; +import java.sql.SQLException; +import java.util.List; + +public class Main { + public static void main(String[] args) { + ProjectUtil.createDBIfNotExist(); + + List allFiles = ProjectUtil.getAllFiles(); + List allSuccessData = null; + + try { + allSuccessData = ProjectUtil.getAllSuccessData(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + + if (allSuccessData == null) { + return; + } + + // judge if exist in dir, if not add it, otherwise ignore. + int fromIndex = WebDavConfig.getHomePath().length(); + for (File cur : allFiles) { + String relativeName = cur.getAbsolutePath().substring(fromIndex).replaceAll("\\\\", "/"); + String dbName = WebDavConfig.getRoot() + relativeName; + + long count = allSuccessData.stream().filter(db -> db.contains(dbName)).count(); + System.out.print(dbName + " : "); + if (count == 0) { + WebDavUtil.uploadFile(cur, dbName); + ProjectUtil.insertSuccessData(dbName); + } else { + System.out.println(" Ignored"); + } + } + + } +} diff --git a/src/main/java/top/dreamcenter/webdav/prop/Constants.java b/src/main/java/top/dreamcenter/webdav/prop/Constants.java new file mode 100644 index 0000000..faad371 --- /dev/null +++ b/src/main/java/top/dreamcenter/webdav/prop/Constants.java @@ -0,0 +1,11 @@ +package top.dreamcenter.webdav.prop; + +/** + * 全局常量定义 + */ +public class Constants { + /** + * 存表名 + */ + public static final String TABLE_NAME = "file_monitor"; +} diff --git a/src/main/java/top/dreamcenter/webdav/prop/WebDavConfig.java b/src/main/java/top/dreamcenter/webdav/prop/WebDavConfig.java new file mode 100644 index 0000000..713eb60 --- /dev/null +++ b/src/main/java/top/dreamcenter/webdav/prop/WebDavConfig.java @@ -0,0 +1,66 @@ +package top.dreamcenter.webdav.prop; + +import java.io.FileReader; +import java.io.IOException; +import java.util.Properties; + +/** + * Webdav配置 + */ +public class WebDavConfig { + /** + * url地址 + */ + private final static String url; + /** + * 账户 + */ + private final static String account; + /** + * 密钥 + */ + private final static String password; + /** + * 同步的根目录 + */ + private final static String homePath; + /** + * dav根目录 + */ + private final static String root; + + static { + Properties properties = new Properties(); + try { + properties.load(new FileReader("config.properties")); + } catch (IOException e) { + e.printStackTrace(); + } + + url = properties.getProperty("url"); + account = properties.getProperty("account"); + password = properties.getProperty("password"); + homePath = properties.getProperty("homePath"); + root = properties.getProperty("root"); + } + + public static String getUrl() { + return url; + } + + public static String getAccount() { + return account; + } + + public static String getPassword() { + return password; + } + + public static String getHomePath() { + return homePath; + } + + public static String getRoot() { + return root; + } +} diff --git a/src/main/java/top/dreamcenter/webdav/util/ProjectUtil.java b/src/main/java/top/dreamcenter/webdav/util/ProjectUtil.java new file mode 100644 index 0000000..e1faaac --- /dev/null +++ b/src/main/java/top/dreamcenter/webdav/util/ProjectUtil.java @@ -0,0 +1,82 @@ +package top.dreamcenter.webdav.util; + +import top.dreamcenter.webdav.prop.Constants; +import top.dreamcenter.webdav.prop.WebDavConfig; + +import java.io.File; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +/** + * 项目工具 + */ +public class ProjectUtil { + + /** + * 初始化建表 + */ + public static void createDBIfNotExist(){ + SqlLiteUtil instance = SqlLiteUtil.getInstance(); + String sql = "CREATE TABLE IF NOT EXISTS " + Constants.TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, fileName TEXT, status INTEGER)"; + instance.execute(sql); + } + + /** + * 获取数据库中所有记录已存的文件 + * @return 所有已记录的文件 + * @throws SQLException 查询异常 + */ + public static List getAllSuccessData() throws SQLException { + SqlLiteUtil sqlLiteUtil = SqlLiteUtil.getInstance(); + Connection connection = sqlLiteUtil.getConnection(); + PreparedStatement statement = connection.prepareStatement("SELECT fileName FROM " + Constants.TABLE_NAME + " WHERE status = 1"); + ResultSet resultSet = statement.executeQuery(); + + List list = new LinkedList<>(); + while (resultSet.next()) { + String fileName = resultSet.getString("fileName"); + list.add(fileName); + } + + connection.close(); + return list; + } + + /** + * 插入一条新的已存文件 + * @param dbName 文件记录名称 + */ + public static void insertSuccessData(String dbName) { + SqlLiteUtil sqlLiteUtil = SqlLiteUtil.getInstance(); + String sql = String.format("INSERT INTO %s (fileName, status) VALUES ('%s', 1)", Constants.TABLE_NAME, dbName); + sqlLiteUtil.execute(sql); + } + + public static List getAllFiles() { + List list = new LinkedList<>(); + + File file = new File(WebDavConfig.getHomePath()); + + Queue dirQueue = new LinkedList<>(); + dirQueue.add(file); + + while (!dirQueue.isEmpty()) { + File tmp = dirQueue.remove(); + + File[] files = tmp.listFiles(); + if (files != null) { + for (File curFile : files) { + if (curFile.isDirectory()) dirQueue.add(curFile); + else list.add(curFile); + } + } + } + + return list; + } +} diff --git a/src/main/java/top/dreamcenter/webdav/util/SqlLiteUtil.java b/src/main/java/top/dreamcenter/webdav/util/SqlLiteUtil.java new file mode 100644 index 0000000..4f8aff3 --- /dev/null +++ b/src/main/java/top/dreamcenter/webdav/util/SqlLiteUtil.java @@ -0,0 +1,60 @@ +package top.dreamcenter.webdav.util; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * sqlite工具 + */ +public class SqlLiteUtil { + + private static volatile SqlLiteUtil instance; + + private SqlLiteUtil(){ + try { + Class.forName("org.sqlite.JDBC"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + /** + * 获取单例实例 + * @return SqlLiteUtil实例 + */ + public static SqlLiteUtil getInstance() { + if (instance == null){ + synchronized (SqlLiteUtil.class) { + if (instance == null) { + instance = new SqlLiteUtil(); + } + } + } + return instance; + } + + /** + * 获取一个连接,主要用于查询 + * @return 连接connection + * @throws SQLException 创建连接失败 + */ + public Connection getConnection() throws SQLException { + return DriverManager.getConnection("jdbc:sqlite:directory.db"); + } + + /** + * 执行sql语句,不需要返回值 + * @param sql sql语句 + */ + public void execute(String sql) { + try (Connection connection = getConnection()) { + PreparedStatement preparedStatement = connection.prepareStatement(sql); + preparedStatement.execute(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + +} diff --git a/src/main/java/top/dreamcenter/webdav/util/WebDavUtil.java b/src/main/java/top/dreamcenter/webdav/util/WebDavUtil.java new file mode 100644 index 0000000..a123af6 --- /dev/null +++ b/src/main/java/top/dreamcenter/webdav/util/WebDavUtil.java @@ -0,0 +1,67 @@ +package top.dreamcenter.webdav.util; + +import org.apache.http.HttpEntity; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.FileEntity; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import top.dreamcenter.webdav.prop.WebDavConfig; + +import java.io.File; + +/** + * WebDav工具 + */ +public class WebDavUtil { + + /** + * 上传更新文件 + * @param file 具体待上传的文件 + * @param davName dav拼接的相对文件路径和文件名 + */ + public static void uploadFile(File file, String davName) { + // 创建HttpClient实例 + CredentialsProvider provider = new BasicCredentialsProvider(); + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(WebDavConfig.getAccount(), WebDavConfig.getPassword()); + provider.setCredentials(AuthScope.ANY, credentials); + CloseableHttpClient client = HttpClients.custom() + .setDefaultCredentialsProvider(provider) + .build(); + try { + // 创建PUT请求 + HttpPut httpPut = new HttpPut(WebDavConfig.getUrl() + "/" + davName); + FileEntity fileEntity = new FileEntity(file, ContentType.DEFAULT_BINARY); // 或者根据需要选择正确的ContentType + httpPut.setEntity(fileEntity); + // 执行请求 + try (CloseableHttpResponse response = client.execute(httpPut)) { + // 检查响应状态码 + System.out.print(response.getStatusLine().getStatusCode()); + HttpEntity responseEntity = response.getEntity(); + if (responseEntity != null) { + System.out.println(" " + responseEntityToString(responseEntity)); + } else { + System.out.println(" Null"); + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + client.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private static String responseEntityToString(HttpEntity entity) throws Exception { + return EntityUtils.toString(entity, "UTF-8"); // 需要Apache HttpCore的依赖来使用这个方法 + } +} diff --git a/src/test/java/top/dreamcenter/AppTest.java b/src/test/java/top/dreamcenter/AppTest.java new file mode 100644 index 0000000..692fbc4 --- /dev/null +++ b/src/test/java/top/dreamcenter/AppTest.java @@ -0,0 +1,26 @@ +package top.dreamcenter; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; +import top.dreamcenter.webdav.prop.Constants; +import top.dreamcenter.webdav.util.SqlLiteUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * Unit test for simple App. + */ +public class AppTest { + + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() { + assertTrue(true); + } + +}