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 name;
String path; String path;
int size; int size;
String downloadUrl;
GiteaTree(); GiteaTree();
factory GiteaTree.fromJson(Map<String, dynamic> json) => factory GiteaTree.fromJson(Map<String, dynamic> json) =>
_$GiteaTreeFromJson(json); _$GiteaTreeFromJson(json);

View File

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

View File

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

View File

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

View File

@ -58,37 +58,11 @@ class ObjectScreen extends StatelessWidget {
return ObjectTree( return ObjectTree(
items: items.map((v) { items: items.map((v) {
// if (item.type == 'commit') return null; // 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( return ObjectTreeItem(
name: v.name, name: v.name,
type: v.type, type: v.type,
url: url, url: '/$owner/$name/blob/$ref?path=${v.path.urlencode}',
downloadUrl: v.downloadUrl,
size: v.type == 'file' ? v.size : null, size: v.type == 'file' ? v.size : null,
); );
}), }),

View File

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

View File

@ -7,12 +7,14 @@ import 'package:seti/seti.dart';
class ObjectTreeItem { class ObjectTreeItem {
final String url; final String url;
final String downloadUrl;
final String name; final String name;
final String type; final String type;
final int size; final int size;
ObjectTreeItem({ ObjectTreeItem({
@required this.name, @required this.name,
@required this.url, @required this.url,
@required this.downloadUrl,
@required this.type, @required this.type,
this.size, this.size,
}); });
@ -55,7 +57,20 @@ class ObjectTree extends StatelessWidget {
leftWidget: _buildIcon(item), leftWidget: _buildIcon(item),
text: Text(item.name), text: Text(item.name),
rightWidget: item.size == null ? null : Text(filesize(item.size)), 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, hideRightChevron: item.size != null,
) )
], ],