Browse Source

Big code refactoring to improve readability and maintenance

master
Figg 1 month ago
parent
commit
20342e34d5
1 changed files with 53 additions and 79 deletions
  1. 53
    79
      ArrangePictures.ps1

+ 53
- 79
ArrangePictures.ps1 View File

@@ -1,5 +1,4 @@
1
-# Fonction pour récupérer une Array des extensions d'images et vidéos depuis un json
2
-function Get-ImageVideoExtensions {
1
+function Get-APImageVideoExtensions {
3 2
     param (
4 3
         [string] $sourceUrl = "https://cdn.jsdelivr.net/gh/jshttp/mime-db@v1.52.0/db.json"
5 4
     )
@@ -7,7 +6,7 @@ function Get-ImageVideoExtensions {
7 6
     $extensions = @()
8 7
 
9 8
     foreach ($item in $jsonData) {
10
-        if ($item.Name -like "image*" -or $item.Name -like "video*") {
9
+        if ($item.Name -match "^(image|video)") {
11 10
             $extensions += $item.Value.extensions
12 11
         }
13 12
     }
@@ -16,113 +15,88 @@ function Get-ImageVideoExtensions {
16 15
     return $extensions
17 16
 }
18 17
 
19
-# Fonction pour décompresser les archives
20
-function Decompress-ArchiveIfNeeded {
21
-    param(
22
-        [string]$ArchivePath,
23
-        [string]$ExtractPath
18
+function Get-APFileDateTaken {
19
+    param (
20
+        [string] $FilePath
24 21
     )
22
+    $Folder = (New-Object -ComObject Shell.Application).NameSpace((Split-Path $FilePath))
23
+    $PropIndex = 12  # Date Taken Property Index
24
+    $CharWhiteList = '[^: \w\/]'
25
+
26
+    $DateTaken = $Folder.GetDetailsOf(
27
+        $Folder.ParseName((Split-Path -Leaf $FilePath)),
28
+        $PropIndex
29
+    ) -replace $CharWhiteList
30
+
25 31
     
26
-    if (Test-Path $ArchivePath -PathType Leaf) {
27
-        $tempDir = Join-Path -Path $ExtractPath -ChildPath ([System.IO.Path]::GetRandomFileName())
28
-        while (Test-Path $tempDir) {
29
-            $tempDir = Join-Path -Path $ExtractPath -ChildPath ([System.IO.Path]::GetRandomFileName())
30
-        }
31
-        New-Item -Path $tempDir -ItemType Directory | Out-Null
32
-        Expand-Archive -Path $ArchivePath -DestinationPath $tempDir
33
-        return $tempDir
34
-    }
35
-    return $null
32
+    $DateTaken = try { [DateTime]($DateTaken) } catch { $null }
33
+    return $DateTaken
34
+}
35
+
36
+function Get-APTemporaryDirectory {
37
+    do {
38
+        $TempDir = Join-Path -Path $env:TEMP -ChildPath ([System.IO.Path]::GetRandomFileName())
39
+    } while (Test-Path $TempDir)
40
+    return $TempDir
36 41
 }
37 42
 
38
-# Fonction pour obtenir la date de création d'une image/vidéo
39
-function Get-OriginalDate {
43
+function Get-APCreationDateSubDirectory {
40 44
     param (
41
-        $file
45
+        [string] $filePath
42 46
     )
43
-    # Tentative de lecture des données EXIF
44
-    $exif = Try { [System.Drawing.Image]::FromFile($file.FullName) } Catch { $null }
45 47
 
46
-    if ($exif) {
47
-        $exifDate = $exif.PropertyItems | Where-Object { $_.Id -eq 0x9003 } | ForEach-Object {
48
-            [System.Text.Encoding]::ASCII.GetString($_.Value).TrimEnd([char]0)
49
-        }
50
-        if ($exifDate) {
51
-            $date = [datetime]::ParseExact($exifDate, "yyyy:MM:dd HH:mm:ss", $null)
52
-        } else {
53
-            $date = $file.CreationTime
54
-        }
55
-        $exif.Dispose()
48
+    $originalDate = Get-APFileDateTaken -FilePath $filePath
49
+
50
+    if ($originalDate) {
51
+        $year = $originalDate.Year
52
+        $month = $originalDate.Month.ToString("D2")
53
+        $monthName = Get-Culture | ForEach-Object { $_.DateTimeFormat.GetMonthName($month) }
54
+
55
+        return "$year\$month - $monthName"
56 56
     } else {
57
-        $date = $file.CreationTime
57
+        return "Non daté"
58 58
     }
59 59
 }
60 60
 
61
-# Demande des paramètres si non fournis
62 61
 if (-not $args[0] -or -not $args[1]) {
63
-    $SourceDir = Read-Host "Entrez le chemin du dossier source"
64
-    $DestinationDir = Read-Host "Entrez le chemin du dossier destination"
62
+    $SourceDir = Read-Host "Veuillez entrer le chemin du dossier source (ou une archive .zip)"
63
+    $DestinationDir = Read-Host "Veuillez entrer le chemin du dossier destination"
65 64
 } else {
66 65
     $SourceDir = $args[0]
67 66
     $DestinationDir = $args[1]
68 67
 }
69 68
 
70
-# Vérifie si le dossier source est une archive
71
-if ($SourceDir -match "\.zip$") {
72
-    $SourceDir = Decompress-ArchiveIfNeeded -ArchivePath $SourceDir -ExtractPath $env:TEMP
73
-    if (-not $SourceDir) {
69
+if ($SourceDir -imatch "\.zip$") {
70
+    $TempDir = Get-APTemporaryDirectory
71
+
72
+    try {
73
+        Expand-Archive -Path $SourceDir -DestinationPath $TempDir
74
+        $SourceDir = $TempDir
75
+    }catch{
74 76
         Write-Host "L'archive n'a pas pu être décompressée."
75 77
         return
76 78
     }
77 79
 }
78 80
 
79
-$validExtensions = Get-ImageVideoExtensions
80
-
81
-# Parcourt tous les fichiers récursivement dans le dossier source
81
+$ValidExtensions = Get-APImageVideoExtensions
82 82
 Get-ChildItem -Path $SourceDir -Recurse -File | ForEach-Object {
83
-    $file = $_
84
-    $extension = $file.Extension.ToLower().TrimStart('.')
85
-
86
-    if ($extension -in $validExtensions) {
87
-        # Tentative de lecture des données EXIF
88
-        $exif = Try { [System.Drawing.Image]::FromFile($file.FullName) } Catch { $null }
89
-
90
-        if ($exif) {
91
-            $exifDate = $exif.PropertyItems | Where-Object { $_.Id -eq 0x9003 } | ForEach-Object {
92
-                [System.Text.Encoding]::ASCII.GetString($_.Value).TrimEnd([char]0)
93
-            }
94
-            if ($exifDate) {
95
-                $originalDate = [datetime]::ParseExact($exifDate, "yyyy:MM:dd HH:mm:ss", $null)
96
-            } else {
97
-                $originalDate = $file.CreationTime
98
-            }
99
-            $exif.Dispose()
100
-        } else {
101
-            $originalDate = $file.CreationTime
102
-        }
103
-        
104
-        if (-Not $originalDate) {
105
-            $targetFolder = Join-Path -Path $DestinationDir -ChildPath "Non daté"
106
-        } else {
107
-            # Créer le dossier de destination basé sur l'année et le mois
108
-            $year = $originalDate.Year
109
-            $monthNum = $originalDate.Month.ToString("00")
110
-            $monthName = $originalDate.ToString("MMMM", [System.Globalization.CultureInfo]::GetCultureInfo("fr-FR"))
111
-            $targetFolder = Join-Path -Path $DestinationDir -ChildPath "$year\$monthNum - $monthName"
112
-        }
83
+    $File = $_
84
+    $FileExtension = $File.Extension.ToLower().TrimStart('.')
85
+
86
+    if ($ValidExtensions -contains $FileExtension) {
87
+        $subFolder = Get-APCreationDateSubDirectory -filePath $File.FullName
88
+        $targetDir = Join-Path -Path $DestinationDir -ChildPath $subFolder
113 89
 
114
-        if (-Not (Test-Path $targetFolder)) {
115
-            New-Item -Path $targetFolder -ItemType Directory | Out-Null
90
+        if (-not (Test-Path $targetDir)) {
91
+            New-Item -Path $targetDir -ItemType Directory | Out-Null
116 92
         }
117 93
 
118
-        # Copier le fichier dans le dossier de destination
119
-        Copy-Item -Path $file.FullName -Destination $targetFolder
94
+        Copy-Item -Path $file.FullName -Destination $targetDir
120 95
     } else {
121 96
         Write-Host "Fichier ignoré: $($file.FullName)"
122 97
     }
123 98
 }
124 99
 
125
-# Suppression du dossier temporaire si nécessaire
126 100
 if ($SourceDir -like "$env:TEMP\*") {
127 101
     Remove-Item -Path $SourceDir -Recurse -Force
128 102
 }

Loading…
Cancel
Save