mirror of
https://github.com/inventree/inventree-app.git
synced 2025-06-15 03:35:28 +00:00
Upload part attachnents
- Select file from device - Take picture with camera
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:inventree/api.dart';
|
||||
@ -8,6 +9,7 @@ import 'package:inventree/widget/dialogs.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../l10.dart';
|
||||
import '../api_form.dart';
|
||||
@ -506,6 +508,24 @@ class InvenTreeAttachment extends InvenTreeModel {
|
||||
}
|
||||
|
||||
InvenTreeAttachment.fromJson(Map<String, dynamic> json) : super.fromJson(json);
|
||||
|
||||
Future<bool> uploadAttachment(File attachment, {String comment = "", Map<String, String> fields = const {}}) async {
|
||||
|
||||
final http.StreamedResponse response = await InvenTreeAPI().uploadFile(
|
||||
URL,
|
||||
attachment,
|
||||
method: 'POST',
|
||||
name: 'attachment',
|
||||
fields: fields
|
||||
);
|
||||
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
2
lib/l10n
2
lib/l10n
Submodule lib/l10n updated: 20dce387ab...faec97124b
@ -1,10 +1,15 @@
|
||||
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:inventree/inventree/part.dart';
|
||||
import 'package:inventree/widget/refreshable_state.dart';
|
||||
import 'package:inventree/widget/snacks.dart';
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import '../api.dart';
|
||||
import '../l10.dart';
|
||||
@ -36,11 +41,21 @@ class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget
|
||||
|
||||
List<Widget> actions = [];
|
||||
|
||||
if (false && InvenTreeAPI().checkPermission('part', 'change')) {
|
||||
if (InvenTreeAPI().checkPermission('part', 'change')) {
|
||||
|
||||
// File upload
|
||||
actions.add(
|
||||
IconButton(
|
||||
icon: FaIcon(FontAwesomeIcons.plus),
|
||||
onPressed: null,
|
||||
icon: FaIcon(FontAwesomeIcons.fileUpload),
|
||||
onPressed: uploadFile,
|
||||
)
|
||||
);
|
||||
|
||||
// Upload from camera
|
||||
actions.add(
|
||||
IconButton(
|
||||
icon: FaIcon(FontAwesomeIcons.camera),
|
||||
onPressed: uploadFromCamera,
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -48,6 +63,62 @@ class _PartAttachmentDisplayState extends RefreshableState<PartAttachmentsWidget
|
||||
return actions;
|
||||
}
|
||||
|
||||
Future<void> upload(File file) async {
|
||||
final bool result = await InvenTreePartAttachment().uploadAttachment(
|
||||
file,
|
||||
fields: {
|
||||
"part": "${part.pk}"
|
||||
}
|
||||
);
|
||||
|
||||
if (result) {
|
||||
showSnackIcon(L10().uploadSuccess, success: true);
|
||||
} else {
|
||||
showSnackIcon(L10().uploadFailed, success: false);
|
||||
}
|
||||
|
||||
refresh();
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Select a file from the device to upload
|
||||
*/
|
||||
Future<void> uploadFile() async {
|
||||
|
||||
final FilePickerResult? result = await FilePicker.platform.pickFiles();
|
||||
|
||||
if (result != null) {
|
||||
|
||||
String? path = result.files.single.path;
|
||||
|
||||
if (path != null) {
|
||||
File attachment = File(path);
|
||||
|
||||
upload(attachment);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Upload an attachment by taking a new picture with the built in device camera
|
||||
*/
|
||||
Future<void> uploadFromCamera() async {
|
||||
|
||||
|
||||
final picker = ImagePicker();
|
||||
|
||||
final pickedImage = await picker.getImage(source: ImageSource.camera);
|
||||
|
||||
if (pickedImage != null) {
|
||||
File? attachment = File(pickedImage.path);
|
||||
upload(attachment);
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> request() async {
|
||||
|
||||
|
Reference in New Issue
Block a user