Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellet committed Dec 4, 2023
1 parent ea8186b commit 0c13254
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 53 deletions.
75 changes: 27 additions & 48 deletions gal_linux/lib/src/gal_linux_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,10 @@ final class GalLinuxImpl {
}) async {
try {
final file = File(path);
final fileExists = await file.exists();
var filePath = path;

bool downloadedFromNetwork = false;

// Download from network
if (!fileExists) {
final fileName = _baseName(path);
if (!await file.exists()) {
final uri = Uri.parse(path);

// If it not exists and it also doesn't starts with https
Expand All @@ -91,55 +88,35 @@ final class GalLinuxImpl {
}

// Save it to a temp directory for now
final tempFileLocation = _getNewTempFileLocation(fileName: fileName);
await executeCommand(
executalbe: 'wget',
args: [
'-O',
tempFileLocation,
path,
],
);
final tempFileLocation =
_getNewTempFileLocation(fileName: _baseName(path));
await executeCommand('wget -O $tempFileLocation $path');
downloadedFromNetwork = true;
filePath = tempFileLocation;
path = tempFileLocation;
}

final fileName = _baseName(filePath);

// Save it to the album
if (album != null) {
final newFileLocation = _getNewFileLocationWithAlbum(
fileType: fileType,
album: album,
fileName: fileName,
fileName: _baseName(path),
);
await _makeSureParentFolderExists(path: newFileLocation);
await executeCommand(
executalbe: 'mv',
args: [
filePath,
newFileLocation,
],
'mv $path $newFileLocation',
);
} else {
// Save it in temp directory
final newFileLocation = _getNewTempFileLocation(fileName: fileName);
final newFileLocation =
_getNewTempFileLocation(fileName: _baseName(path));
await _makeSureParentFolderExists(path: newFileLocation);
await executeCommand(
executalbe: 'mv',
args: [
filePath,
newFileLocation,
],
);
await executeCommand('mv $path $newFileLocation');
}
// Remove the downloaded file from the network if it exists
if (downloadedFromNetwork) {
await executeCommand(
executalbe: 'rm',
args: [
filePath,
],
'rm $path',
);
}
} on ProcessException catch (e) {
Expand Down Expand Up @@ -167,33 +144,36 @@ final class GalLinuxImpl {
}
}

static String _getHomeDirectory() =>
Platform.environment['HOME'] ??
(throw UnsupportedError(
'The HOME environment variable is null and it is required'));

static String _getNewFileLocationWithAlbum({
required _FileType fileType,
required String album,
required String fileName,
}) {
final currentDate = DateTime.now().toIso8601String();
final newFileLocation = switch (fileType) {
_FileType.image => '~/Pictures/$album/$fileName',
_FileType.video => '~/Videos/$album/$fileName',
_FileType.image =>
'${_getHomeDirectory()}/Pictures/$album/$currentDate-$fileName',
_FileType.video =>
'${_getHomeDirectory()}/Videos/$album/$currentDate-$fileName',
};
return newFileLocation;
}

static String _getNewTempFileLocation({
required String fileName,
}) {
return '${Directory.systemTemp.path}/gal/${DateTime.now().toIso8601String()}-$fileName';
final currentDate = DateTime.now().toIso8601String();
return '${Directory.systemTemp.path}/gal/$currentDate-$fileName';
}

static Future<void> _makeSureParentFolderExists(
{required String path}) async {
await executeCommand(
executalbe: 'mkdir',
args: [
'-p',
File(path).parent.path,
],
);
await executeCommand('mkdir -p ${File(path).parent.path}');
}

/// Save a image to the gallery from [Uint8List].
Expand All @@ -218,9 +198,8 @@ final class GalLinuxImpl {
/// Open gallery app.
///
/// If there are multiple gallery apps, App selection sheet may be displayed.
static Future<void> open() async => throw UnsupportedError(
'Linux is not supported yet.',
);
static Future<void> open() async =>
executeCommand('xdg-open ${_getHomeDirectory()}/Pictures');

/// Check if the app has access permissions.
///
Expand Down
18 changes: 13 additions & 5 deletions gal_linux/lib/src/utils/command_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ import 'dart:io' show ProcessException, Process;

import 'package:flutter/foundation.dart' show kDebugMode, kIsWeb;

Future<String> executeCommand({
required String executalbe,
List<String> args = const [],
Future<String> executeCommand(
String value, {
bool printResult = true,
String? workingDirectory,
}) async {
final executalbe = value.split(' ')[0];
final args = value.split(' ')
..removeAt(0)
..toList();
if (kDebugMode) {
if (printResult) {
print('\n command: $executalbe ${args.join(' ')}');
print('$executalbe ${args.join(' ')}');
}
}
if (kIsWeb) {
throw UnsupportedError(
'The command line is not supported on web',
);
}
final command = await Process.run(executalbe, args);
final command = await Process.run(
executalbe,
args,
workingDirectory: workingDirectory,
);
if (command.exitCode != 0) {
if (kDebugMode) {
if (printResult) {
Expand Down

0 comments on commit 0c13254

Please sign in to comment.