diff --git a/NextCloudTools.sln b/NextCloudTools.sln new file mode 100644 index 0000000..6fe2daf --- /dev/null +++ b/NextCloudTools.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34221.43 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NextCloudTools", "NextCloudTools\NextCloudTools.csproj", "{62DC51FB-828C-494C-BF05-AA7E19081F28}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {62DC51FB-828C-494C-BF05-AA7E19081F28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {62DC51FB-828C-494C-BF05-AA7E19081F28}.Debug|Any CPU.Build.0 = Debug|Any CPU + {62DC51FB-828C-494C-BF05-AA7E19081F28}.Release|Any CPU.ActiveCfg = Release|Any CPU + {62DC51FB-828C-494C-BF05-AA7E19081F28}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {788F40DA-2472-4B3D-9529-E2BCA559E99D} + EndGlobalSection +EndGlobal diff --git a/NextCloudTools/NextCloudTools.csproj b/NextCloudTools/NextCloudTools.csproj new file mode 100644 index 0000000..7a8e1af --- /dev/null +++ b/NextCloudTools/NextCloudTools.csproj @@ -0,0 +1,15 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + + diff --git a/NextCloudTools/Program.cs b/NextCloudTools/Program.cs new file mode 100644 index 0000000..736cb5d --- /dev/null +++ b/NextCloudTools/Program.cs @@ -0,0 +1,68 @@ +// See https://aka.ms/new-console-template for more information +using System.IO.Compression; +using NextCloudTools; +using SharpCompress.Readers.Zip; + +string downloadPath = "D:\\Users\\narit\\Downloads\\"; +string photoImageRoot = "D:\\Public\\Photo\\"; + +List imageExtensions = new List(); +imageExtensions.Add(".jpg"); +imageExtensions.Add(".jpeg"); +imageExtensions.Add(".png"); +imageExtensions.Add(".mp4"); + +foreach (var zipFileName in Directory.GetFiles(downloadPath, "*.zip")) +{ + Console.WriteLine(zipFileName); + string photoTempRoot = Path.TrimEndingDirectorySeparator(Path.GetTempPath()) + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyyMMddHHmmssff") + Path.DirectorySeparatorChar; + using (var archive = new ZipArchive(File.OpenRead(zipFileName), ZipArchiveMode.Read)) + { + // ZIPファイルをテンポラリに展開 + archive.ExtractToDirectory(photoTempRoot, true); + archive.Dispose(); + + // ファイルの内容を確認する + bool haveImageFile = false; + DateTime lastAccessTimeMost = DateTime.MaxValue; + foreach (var imageFile in Directory.GetFiles(photoTempRoot)) + { + string extension = Path.GetExtension(imageFile); + if (imageExtensions.Contains(extension)) + { + haveImageFile = true; + var lastAccessTime = File.GetLastWriteTime(imageFile); + if (lastAccessTimeMost > lastAccessTime) + { + lastAccessTimeMost = lastAccessTime; + } + } + } + + // ファイルを保存場所に移動する + if (haveImageFile) + { + string imageTitle = Path.GetFileName(zipFileName); + imageTitle = imageTitle.Substring(0, imageTitle.LastIndexOf('.')); + string photoImagePath = photoImageRoot + lastAccessTimeMost.ToString("yyyyMMdd") + " " + imageTitle + Path.DirectorySeparatorChar; + + Directory.CreateDirectory(photoImagePath); + foreach (var imageFile in Directory.GetFiles(photoTempRoot)) + { + File.Copy(imageFile, photoImagePath + Path.GetFileName(imageFile), true); + } + + // 圧縮ファイルを廃棄 + File.Delete(zipFileName); + + } + + // テンポラリを廃棄 + Directory.Delete(photoTempRoot, true); + } +} + +; + +WebDavClient client = new WebDavClient() { userName = "knarita", password = "7EF88-6iJEz-mXNdQ-Crocg-mepKT", hostName = "https://next.code-lab.net/remote.php/dav/files/knarita" }; +client.GetFileList(); \ No newline at end of file diff --git a/NextCloudTools/WebDavClient.cs b/NextCloudTools/WebDavClient.cs new file mode 100644 index 0000000..17ef11d --- /dev/null +++ b/NextCloudTools/WebDavClient.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +using WinSCP; + +namespace NextCloudTools +{ + internal class WebDavClient + { + public string userName { get; set; } + public string password { get; set; } + public string hostName { get; set; } + + public WebDavClient() + { + userName = ""; + password = ""; + hostName = ""; + } + + + public void GetFileList() + { + SessionOptions sessionOptions = new SessionOptions(); + sessionOptions.UserName = userName; + sessionOptions.Password = password; + sessionOptions.Protocol = Protocol.Webdav; + sessionOptions.HostName = "next.code-lab.net"; + sessionOptions.RootPath = "/remote.php/dav/files/knarita"; + + Session WebDavSession = new Session(); + try + { + WebDavSession.Open(sessionOptions); + TransferOptions TransferOptions = new TransferOptions(); + TransferOptions.TransferMode = TransferMode.Automatic; + TransferOperationResult transferResult; + transferResult = WebDavSession.GetFiles("目標設定", "D:\\"); + transferResult.Check(); + } + catch (Exception e) + { + throw e; + } + finally + { + WebDavSession.Close(); + } + } + } + +/* + public static void SendFileWebDav(string FileToSend) + { + if (!DoesFileExist(FileToSend)) + { + throw new Exception(string.Format("Cannot Find File {0}", FileToSend)); + } + SessionOptions sessionOptions = new SessionOptions(); + sessionOptions.UserName = WebDavUploadSettings.Default.Username; + sessionOptions.Password = WebDavUploadSettings.Default.Password; + if (string.IsNullOrWhiteSpace(WebDavUploadSettings.Default.FolderPathToPushTo)) //WINSCP will throw an error if root directory doesn't start with / + { + //Haven't tested this. + sessionOptions.WebdavRoot = "/"; + } + else + { + sessionOptions.WebdavRoot = WebDavUploadSettings.Default.FolderPathToPushTo.ToString(); //Added .ToString() here because I was getting 404 error other wise? Maybe because is was not reading in the setting correctly. + } + //Make sure you define this as a URL without protocal. (Leave off Https:// and Http://) + sessionOptions.HostName = WebDavUploadSettings.Default.Website; + sessionOptions.Protocol = Protocol.Webdav; + Session WebDavSession = new Session(); + + try + { + WebDavSession.Open(sessionOptions); + TransferOptions TransferOptions = new TransferOptions(); + TransferOptions.TransferMode = TransferMode.Automatic; + TransferOperationResult transferResult; + transferResult = WebDavSession.PutFiles(FileToSend, Path.GetFileName(FileToSend), false, TransferOptions); + transferResult.Check(); + } + catch (Exception e) + { + throw e; + } + finally + { + WebDavSession.Close(); + } + } +*/ +}