Skip to content

Commit

Permalink
file upload works fine
Browse files Browse the repository at this point in the history
  • Loading branch information
LiquidatorCoder2 committed Nov 13, 2021
1 parent cd95d6c commit 67135c2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
38 changes: 27 additions & 11 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'dart:io';

import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter/material.dart';
import 'package:odin/services/data_service.dart';
import 'package:odin/widgets/window_top_bar.dart';

const backgroundStartColor = Color(0x55121212);
Expand All @@ -13,8 +16,10 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
final List<Uri> _list = [];
bool _dragging = false;
bool _loading = false;
String? _fileLink;
final _ds = DataService();

@override
Widget build(BuildContext context) {
Expand All @@ -31,9 +36,14 @@ class _HomePageState extends State<HomePage> {
child: Stack(
children: [
DropTarget(
onDragDone: (detail) {
onDragDone: (detail) async {
setState(() {
_loading = true;
});
_fileLink = await _ds
.uploadFileAnonymous(File(detail.urls.first.toFilePath()));
setState(() {
_list.addAll(detail.urls);
_loading = false;
});
},
onDragEntered: (detail) {
Expand All @@ -54,14 +64,20 @@ class _HomePageState extends State<HomePage> {
? Colors.blue.withOpacity(0.2)
: Colors.transparent,
child: Center(
child: Text(
_list.isEmpty ? 'Drop a file to start' : _list.join("\n"),
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w100,
color: Colors.white.withOpacity(0.3),
),
),
child: _loading
? CircularProgressIndicator(
backgroundColor: Colors.white.withOpacity(0.3),
)
: Text(
_fileLink == null
? 'Drop a file to start'
: _fileLink.toString(),
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w100,
color: Colors.white.withOpacity(0.3),
),
),
),
),
),
Expand Down
12 changes: 6 additions & 6 deletions lib/services/data_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import 'dart:io';

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:github/github.dart';
import 'package:intl/intl.dart';
import 'package:path/path.dart' as path;

class DataService {
final _env = dotenv.env;
final _gh =
GitHub(auth: Authentication.withToken(dotenv.env['GITHUB_TOKEN']));

Future<void> uploadFileAnonymous(File file) async {
await _gh.repositories.createFile(
Future<String> uploadFileAnonymous(File file) async {
final uploadTime = DateFormat('dd-MM-yyyy hh:mm:ss').format(DateTime.now());
final _ghFile = await _gh.repositories.createFile(
RepositorySlug(
_env['GITHUB_USERNAME'] ?? '', _env['GITHUB_REPO_NAME'] ?? ''),
CreateFile(
branch: _env['GITHUB_BRANCH_NAME'],
committer: CommitUser(
_env['GITHUB_COMMIT_USER_NAME'], _env['GITHUB_COMMIT_USER_EMAIL']),
content: base64Encode(file.readAsBytesSync()),
message: "☄️ -> '${path.basename(file.path)}'",
message: "☄️ -> '${path.basename(file.path)}' | $uploadTime",
path: path.basename(file.path),
),
);
return _ghFile.content?.downloadUrl ?? '';
}
}
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
intl:
dependency: "direct main"
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.0"
json_annotation:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies:
github: ^8.2.5
path: ^1.8.0
flutter_dotenv: ^5.0.2
intl: ^0.17.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 67135c2

Please sign in to comment.