refactor: add download url

This commit is contained in:
Rongjian Zhang 2020-01-31 16:40:20 +08:00
parent fe8bf0a800
commit eddcb1fc39
7 changed files with 30 additions and 34 deletions

View File

@ -38,6 +38,7 @@ class GiteaTree {
String name;
String path;
int size;
String downloadUrl;
GiteaTree();
factory GiteaTree.fromJson(Map<String, dynamic> json) =>
_$GiteaTreeFromJson(json);

View File

@ -64,7 +64,8 @@ GiteaTree _$GiteaTreeFromJson(Map<String, dynamic> json) {
..type = json['type'] as String
..name = json['name'] as String
..path = json['path'] as String
..size = json['size'] as int;
..size = json['size'] as int
..downloadUrl = json['download_url'] as String;
}
Map<String, dynamic> _$GiteaTreeToJson(GiteaTree instance) => <String, dynamic>{
@ -72,6 +73,7 @@ Map<String, dynamic> _$GiteaTreeToJson(GiteaTree instance) => <String, dynamic>{
'name': instance.name,
'path': instance.path,
'size': instance.size,
'download_url': instance.downloadUrl,
};
GiteaBlob _$GiteaBlobFromJson(Map<String, dynamic> json) {
@ -80,6 +82,7 @@ GiteaBlob _$GiteaBlobFromJson(Map<String, dynamic> json) {
..name = json['name'] as String
..path = json['path'] as String
..size = json['size'] as int
..downloadUrl = json['download_url'] as String
..content = json['content'] as String;
}
@ -88,5 +91,6 @@ Map<String, dynamic> _$GiteaBlobToJson(GiteaBlob instance) => <String, dynamic>{
'name': instance.name,
'path': instance.path,
'size': instance.size,
'download_url': instance.downloadUrl,
'content': instance.content,
};

View File

@ -61,6 +61,7 @@ class GiteaObjectScreen extends StatelessWidget {
type: v.type,
size: v.type == 'file' ? v.size : null,
url: '/gitea/$owner/$name/blob?path=${v.path.urlencode}',
downloadUrl: v.downloadUrl,
),
]);
} else {

View File

@ -20,6 +20,7 @@ class GitlabTreeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final auth = Provider.of<AuthModel>(context);
return RefreshStatefulScaffold<Iterable<GitlabTreeItem>>(
title: AppBarTitle(path ?? 'Files'),
fetchData: () async {
@ -27,7 +28,7 @@ class GitlabTreeScreen extends StatelessWidget {
if (path != null) {
url += '?path=' + path;
}
final res = await Provider.of<AuthModel>(context).fetchGitlab(url);
final res = await auth.fetchGitlab(url);
return (res as List).map((v) => GitlabTreeItem.fromJson(v));
},
bodyBuilder: (data, _) {
@ -36,6 +37,8 @@ class GitlabTreeScreen extends StatelessWidget {
return ObjectTreeItem(
type: item.type,
name: item.name,
downloadUrl:
'${auth.activeAccount.domain}/api/v4/projects/$id/repository/files/${item.path.urlencode}/raw?ref=master', // TODO:
url: (() {
switch (item.type) {
case 'tree':

View File

@ -58,37 +58,11 @@ class ObjectScreen extends StatelessWidget {
return ObjectTree(
items: items.map((v) {
// if (item.type == 'commit') return null;
String url;
if ([
// Docs
'pdf',
'docx',
'doc',
'pptx',
'ppt',
'xlsx',
'xls',
// Fonts
'ttf',
'otf',
'eot',
'woff',
'woff2'
].contains(v.name.ext)) {
// Let system browser handle these files
//
// TODO:
// Unhandled Exception: PlatformException(Error, Error while launching
// https://github.com/flutter/flutter/issues/49162
url = v.downloadUrl;
} else {
url = '/$owner/$name/blob/$ref?path=${v.path.urlencode}';
}
return ObjectTreeItem(
name: v.name,
type: v.type,
url: url,
url: '/$owner/$name/blob/$ref?path=${v.path.urlencode}',
downloadUrl: v.downloadUrl,
size: v.type == 'file' ? v.size : null,
);
}),

View File

@ -23,8 +23,6 @@ class BlobView extends StatelessWidget {
this.networkUrl,
});
String get _language => name.ext ?? 'plaintext';
@override
Widget build(BuildContext context) {
final codeProvider = Provider.of<CodeModel>(context);
@ -58,7 +56,7 @@ class BlobView extends StatelessWidget {
scrollDirection: Axis.horizontal,
child: HighlightView(
base64Text.base64ToUtf8,
language: _language,
language: name.ext ?? 'plaintext',
theme: themeMap[theme.brightness == Brightness.dark
? codeProvider.themeDark
: codeProvider.theme],

View File

@ -7,12 +7,14 @@ import 'package:seti/seti.dart';
class ObjectTreeItem {
final String url;
final String downloadUrl;
final String name;
final String type;
final int size;
ObjectTreeItem({
@required this.name,
@required this.url,
@required this.downloadUrl,
@required this.type,
this.size,
});
@ -55,7 +57,20 @@ class ObjectTree extends StatelessWidget {
leftWidget: _buildIcon(item),
text: Text(item.name),
rightWidget: item.size == null ? null : Text(filesize(item.size)),
url: item.url,
url: [
// Let system browser handle these files
//
// TODO:
// Unhandled Exception: PlatformException(Error, Error while launching
// https://github.com/flutter/flutter/issues/49162
// Docs
'pdf', 'docx', 'doc', 'pptx', 'ppt', 'xlsx', 'xls',
// Fonts
'ttf', 'otf', 'eot', 'woff', 'woff2',
].contains(item.name.ext)
? item.downloadUrl
: item.url,
hideRightChevron: item.size != null,
)
],