mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-05-12 07:08:44 -05:00
[client] Create publinks
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import 'package:offtheline/offtheline.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource_create.dart';
|
||||
import 'package:phylum/libphylum/phylum_api_types.dart';
|
||||
import 'package:phylum/libphylum/responses/responses.dart';
|
||||
|
||||
import 'action_resource.dart';
|
||||
|
||||
class ResourcePublinkCreateAction extends ResourceAction with JsonApiAction {
|
||||
static const kActionName = 'publinkCreate';
|
||||
@override
|
||||
String get actionName => kActionName;
|
||||
|
||||
@override
|
||||
String get method => 'POST';
|
||||
|
||||
@override
|
||||
String get endpoint => '/api/v1/fs/publinks/$resourceId/create';
|
||||
|
||||
@override
|
||||
Map<String, dynamic>? get requestBody => {
|
||||
'name': publinkName,
|
||||
if (password.isNotEmpty) 'password': password,
|
||||
if (expires != null) 'expires': expires!.toIso8601String(),
|
||||
if (accessLimit != 0) 'access_limit': accessLimit,
|
||||
};
|
||||
|
||||
@override
|
||||
ResponseParser get parseResponse => (_, response) => parseJsonMapResponse(response, EmptyResponse.fromResponse);
|
||||
|
||||
@override
|
||||
List<LocalChange> get localChanges => [];
|
||||
|
||||
@override
|
||||
String get description => 'Creating Public Share: $publinkName';
|
||||
|
||||
@override
|
||||
Map<String, dynamic> get props => {
|
||||
'resourceId': resourceId,
|
||||
'publinkName': resourceId,
|
||||
'password': password,
|
||||
'expires': expires?.millisecondsSinceEpoch,
|
||||
'accessLimit': accessLimit,
|
||||
'timestamp': timestamp.millisecondsSinceEpoch,
|
||||
};
|
||||
|
||||
final String publinkName;
|
||||
final String password;
|
||||
final DateTime? expires;
|
||||
final int accessLimit;
|
||||
final DateTime timestamp;
|
||||
|
||||
ResourcePublinkCreateAction({
|
||||
required super.resourceId,
|
||||
required this.publinkName,
|
||||
required this.password,
|
||||
required DateTime? expires,
|
||||
required this.accessLimit,
|
||||
DateTime? timestamp,
|
||||
}) : expires = expires?.toUtc(),
|
||||
timestamp = timestamp ?? DateTime.now();
|
||||
|
||||
factory ResourcePublinkCreateAction.fromMap(Map<String, dynamic> map) {
|
||||
return ResourcePublinkCreateAction(
|
||||
resourceId: map['resourceId'],
|
||||
publinkName: map['publinkName'],
|
||||
password: map['password'],
|
||||
expires: map['expires'] == null ? null : DateTime.fromMillisecondsSinceEpoch(map['expires']),
|
||||
accessLimit: map['accessLimit'],
|
||||
timestamp: DateTime.fromMillisecondsSinceEpoch(map['timestamp']),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool dependsOn(PhylumAction action) =>
|
||||
action is ResourceAction &&
|
||||
(action is ResourceCreateAction || action is ResourcePublinkCreateAction) &&
|
||||
action.resourceId == resourceId;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import 'package:phylum/libphylum/actions/action_resource_copy.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource_delete.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource_mkdir.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource_move.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource_publink_create.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource_restore.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource_share.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource_upload.dart';
|
||||
@@ -16,6 +17,7 @@ const actionDeserializers = {
|
||||
ResourceMkdirAction.kActionName: ResourceMkdirAction.fromMap,
|
||||
ResourceMoveAction.kActionName: ResourceMoveAction.fromMap,
|
||||
ResourceRestoreAction.kActionName: ResourceRestoreAction.fromMap,
|
||||
ResourcePublinkCreateAction.kActionName: ResourcePublinkCreateAction.fromMap,
|
||||
ResourceShareAction.kActionName: ResourceShareAction.fromMap,
|
||||
ResourceUploadAction.kActionName: ResourceUploadAction.fromMap,
|
||||
};
|
||||
|
||||
@@ -3,6 +3,8 @@ import 'package:flutter/services.dart';
|
||||
import 'package:phylum/util/time.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
final TextInputFormatter validNameFormatter = FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9_\-]'));
|
||||
|
||||
class PublinkInfo with ChangeNotifier {
|
||||
String _name = '';
|
||||
String get name => _name.trim();
|
||||
@@ -26,7 +28,7 @@ class PublinkInfo with ChangeNotifier {
|
||||
}
|
||||
|
||||
int? _accessLimit;
|
||||
int? get accessLimit => _accessLimit;
|
||||
int get accessLimit => _accessLimit ?? 0;
|
||||
set accessLimit(int? value) {
|
||||
_accessLimit = value;
|
||||
notifyListeners();
|
||||
@@ -97,8 +99,10 @@ class _CreatePublinkViewState extends State<CreatePublinkView> {
|
||||
leading: const Icon(Icons.public),
|
||||
title: TextField(
|
||||
decoration: InputDecoration(label: Text('Name')),
|
||||
inputFormatters: [validNameFormatter],
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
autofocus: true,
|
||||
onChanged: (value) => context.read<PublinkInfo>().name = value,
|
||||
),
|
||||
),
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:offtheline/offtheline.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource.dart';
|
||||
import 'package:phylum/libphylum/actions/action_resource_publink_create.dart';
|
||||
import 'package:phylum/libphylum/db/db.dart';
|
||||
import 'package:phylum/libphylum/phylum_account.dart';
|
||||
import 'package:phylum/ui/common/responsive_dialog.dart';
|
||||
@@ -111,9 +112,16 @@ class ResourceInfoView extends StatelessWidget {
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () async {
|
||||
final account = context.read<PhylumAccount>();
|
||||
final info = await CreatePublinkView.show(context, resource.id);
|
||||
if (info == null) return;
|
||||
print('Created');
|
||||
account.addAction(ResourcePublinkCreateAction(
|
||||
resourceId: resource.id,
|
||||
publinkName: info.name,
|
||||
password: info.password,
|
||||
expires: info.expiration,
|
||||
accessLimit: info.accessLimit,
|
||||
));
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user