master v1.0
dai_48k 2025-03-17 15:12:01 +08:00
commit aa93d2d6d6
10 changed files with 456 additions and 0 deletions

6
.gitignore vendored 100644
View File

@ -0,0 +1,6 @@
config.properties
.mvn
.idea
target
directory.db
webdav.iml

View File

@ -0,0 +1,5 @@
url=https://xxx.xxx/dav/
account=xxxxx@qq.com
password=xxxxxxx
homePath=E:\\data\\test
root=test

88
pom.xml 100644
View File

@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.dreamcenter</groupId>
<artifactId>webdav</artifactId>
<version>1.0-SNAPSHOT</version>
<name>webdav</name>
<url>https://www.dreamcenter.top</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.36.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>top.dreamcenter.webdav.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -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<File> allFiles = ProjectUtil.getAllFiles();
List<String> 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");
}
}
}
}

View File

@ -0,0 +1,11 @@
package top.dreamcenter.webdav.prop;
/**
*
*/
public class Constants {
/**
*
*/
public static final String TABLE_NAME = "file_monitor";
}

View File

@ -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;
}
}

View File

@ -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<String> 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<String> 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<File> getAllFiles() {
List<File> list = new LinkedList<>();
File file = new File(WebDavConfig.getHomePath());
Queue<File> 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;
}
}

View File

@ -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();
}
}
}

View File

@ -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的依赖来使用这个方法
}
}

View File

@ -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);
}
}