-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(package_info_plus): implement install time
for desktop platforms
- Loading branch information
1 parent
4b25c5f
commit 7c16b7b
Showing
8 changed files
with
186 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/package_info_plus/package_info_plus/lib/src/file_attribute.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import 'dart:ffi'; | ||
import 'dart:io'; | ||
|
||
import 'package:ffi/ffi.dart'; | ||
import 'package:win32/win32.dart'; | ||
|
||
base class FILEATTRIBUTEDATA extends Struct { | ||
@DWORD() | ||
external int dwFileAttributes; | ||
|
||
external FILETIME ftCreationTime; | ||
|
||
external FILETIME ftLastAccessTime; | ||
|
||
external FILETIME ftLastWriteTime; | ||
|
||
@DWORD() | ||
external int nFileSizeHigh; | ||
|
||
@DWORD() | ||
external int nFileSizeLow; | ||
} | ||
|
||
class FileAttributes { | ||
final String filePath; | ||
|
||
late final DateTime? creationTime; | ||
late final DateTime? lastWriteTime; | ||
|
||
FileAttributes(this.filePath) { | ||
final attributesPtr = getFileAttributes(filePath); | ||
|
||
if (attributesPtr != null) { | ||
creationTime = fileTimeToDartDateTime(attributesPtr.ref.ftCreationTime); | ||
lastWriteTime = fileTimeToDartDateTime(attributesPtr.ref.ftLastWriteTime); | ||
|
||
free(attributesPtr); | ||
} else { | ||
creationTime = null; | ||
lastWriteTime = null; | ||
} | ||
} | ||
|
||
static Pointer<FILEATTRIBUTEDATA>? getFileAttributes(String filePath) { | ||
if (!File(filePath).existsSync()) { | ||
throw ArgumentError.value(filePath, 'filePath', 'File not present'); | ||
} | ||
|
||
final lptstrFilename = TEXT(filePath); | ||
final lpFileInformation = calloc<FILEATTRIBUTEDATA>(); | ||
|
||
try { | ||
if (GetFileAttributesEx(lptstrFilename, 0, lpFileInformation) == 0) { | ||
free(lpFileInformation); | ||
|
||
return null; | ||
} | ||
|
||
return lpFileInformation; | ||
} finally {} | ||
} | ||
|
||
static DateTime? fileTimeToDartDateTime(FILETIME? fileTime) { | ||
if (fileTime == null) return null; | ||
|
||
final high = fileTime.dwHighDateTime; | ||
final low = fileTime.dwLowDateTime; | ||
|
||
final fileTime64 = (high << 32) + low; | ||
|
||
final unixTimeMs = ((fileTime64 ~/ 10000) - 11644473600000); | ||
|
||
return DateTime.fromMillisecondsSinceEpoch(unixTimeMs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters