Skip to content

Commit

Permalink
add zip archive for multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
codenameakshay committed Nov 14, 2021
1 parent 9fcdf4f commit 2011431
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 31 deletions.
15 changes: 13 additions & 2 deletions lib/pages/home_page.dart
Expand Up @@ -4,6 +4,7 @@ import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter/material.dart';
import 'package:odin/services/data_service.dart';
import 'package:odin/services/locator.dart';
import 'package:odin/services/zip_service.dart';
import 'package:odin/widgets/window_top_bar.dart';
import 'package:url_launcher/url_launcher.dart';

Expand All @@ -22,6 +23,7 @@ class _HomePageState extends State<HomePage> {
bool _loading = false;
String? _fileLink;
final _ds = locator<DataService>();
final _zs = locator<ZipService>();

@override
Widget build(BuildContext context) {
Expand All @@ -44,8 +46,17 @@ class _HomePageState extends State<HomePage> {
_loading = true;
});
if (detail.urls.isNotEmpty) {
_fileLink = await _ds.uploadFileAnonymous(
File(detail.urls.first.toFilePath()));
final int length = detail.urls.length;
if (length > 1) {
final List<File> fileToZips =
detail.urls.map((e) => File(e.toFilePath())).toList();
final zippedFile =
await _zs.zipFile(fileToZips: fileToZips);
_fileLink = await _ds.uploadFileAnonymous(zippedFile);
} else {
_fileLink = await _ds.uploadFileAnonymous(
File(detail.urls.first.toFilePath()));
}
}
setState(() {
_loading = false;
Expand Down
12 changes: 4 additions & 8 deletions lib/services/data_service.dart
@@ -1,25 +1,20 @@
import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:github/github.dart';
import 'package:intl/intl.dart';
import 'package:odin/services/locator.dart';
import 'package:odin/services/random_service.dart';
import 'package:odin/services/shortner_service.dart';
import 'package:path/path.dart' as path;

class DataService {
final ShortnerService _shortnerService = locator<ShortnerService>();
final RandomService _randomService = locator<RandomService>();
final _env = dotenv.env;
final _gh =
GitHub(auth: Authentication.withToken(dotenv.env['GITHUB_TOKEN']));
static const _chars =
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
final Random _rnd = Random();

String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));

Future<String> uploadFileAnonymous(File file) async {
final uploadTime = DateFormat('dd-MM-yyyy hh:mm:ss').format(DateTime.now());
Expand All @@ -29,7 +24,8 @@ class DataService {
CreateFile(
content: base64Encode(file.readAsBytesSync()),
message: "☄️ -> '${path.basename(file.path)}' | $uploadTime",
path: "${getRandomString(15)}/${path.basename(file.path)}",
path:
"${_randomService.getRandomString(15)}/${path.basename(file.path)}",
),
);
final _downloadLink = await _shortnerService.shortUrl(
Expand Down
4 changes: 4 additions & 0 deletions lib/services/locator.dart
@@ -1,6 +1,8 @@
import 'package:get_it/get_it.dart';
import 'package:odin/services/data_service.dart';
import 'package:odin/services/random_service.dart';
import 'package:odin/services/shortner_service.dart';
import 'package:odin/services/zip_service.dart';

import 'logger.dart';

Expand All @@ -11,6 +13,8 @@ Future<void> setupLocator() async {
// locator.registerFactory<CurrentDataNotifier>(() => CurrentDataNotifier());
locator.registerLazySingleton<DataService>(() => DataService());
locator.registerLazySingleton<ShortnerService>(() => ShortnerService());
locator.registerLazySingleton<RandomService>(() => RandomService());
locator.registerLazySingleton<ZipService>(() => ZipService());
logger.d('Locator setup in ${stopwatch.elapsed}');
stopwatch.stop();
}
10 changes: 10 additions & 0 deletions lib/services/random_service.dart
@@ -0,0 +1,10 @@
import 'dart:math';

class RandomService {
static const _chars =
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
final Random _rnd = Random();

String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));
}
28 changes: 28 additions & 0 deletions lib/services/zip_service.dart
@@ -0,0 +1,28 @@
import 'dart:io';
import 'package:archive/archive_io.dart';
import 'package:odin/services/locator.dart';
import 'package:odin/services/random_service.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

class ZipService {
final RandomService _randomService = locator<RandomService>();

Future<File> zipFile({
required List<File> fileToZips,
}) async {
final ZipFileEncoder encoder = ZipFileEncoder();
final Directory zipFileSaveDirectory = await getTemporaryDirectory();
final zipFileSavePath = zipFileSaveDirectory.path;
// Manually create a zip at the zipFilePath
final String zipFilePath =
join(zipFileSavePath, "${_randomService.getRandomString(15)}.zip");
encoder.create(zipFilePath);
// Add all the files to the zip file
for (final File fileToZip in fileToZips) {
encoder.addFile(fileToZip);
}
encoder.close();
return File(zipFilePath);
}
}
14 changes: 14 additions & 0 deletions pubspec.lock
@@ -1,6 +1,13 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: "direct main"
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.6"
async:
dependency: transitive
description:
Expand Down Expand Up @@ -78,6 +85,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
cupertino_icons:
dependency: "direct main"
description:
Expand Down
33 changes: 12 additions & 21 deletions pubspec.yaml
Expand Up @@ -3,7 +3,7 @@ description: A new Flutter project.

# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
publish_to: "none" # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
Expand All @@ -27,43 +27,37 @@ environment:
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter


# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
archive: ^3.1.6
bitsdojo_window: ^0.1.1+1
cupertino_icons: ^1.0.2
desktop_drop: ^0.1.2
github: ^8.2.5
path: ^1.8.0
dio: ^4.0.3
flutter:
sdk: flutter
flutter_dotenv: ^5.0.2
intl: ^0.17.0
provider: ^6.0.1
get_it: ^7.2.0
github: ^8.2.5
intl: ^0.17.0
logger: ^1.1.0
path: ^1.8.0
path_provider: ^2.0.6
provider: ^6.0.1
url_launcher: ^6.0.12
dio: ^4.0.3

dev_dependencies:
flutter_test:
sdk: flutter

# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
flutter_test:
sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
Expand All @@ -73,13 +67,10 @@ flutter:
assets:
- .env
# - images/a_dot_ham.jpeg

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.

# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages

# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
Expand Down

0 comments on commit 2011431

Please sign in to comment.