mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-28 13:36:50 +00:00
Allow upload of part images
- Requires API v6
This commit is contained in:
parent
316391d5fb
commit
9e1305b1f0
15
lib/api.dart
15
lib/api.dart
@ -62,11 +62,10 @@ class InvenTreeFileService extends FileService {
|
||||
class InvenTreeAPI {
|
||||
|
||||
// Minimum required API version for server
|
||||
static const _minApiVersion = 5;
|
||||
static const _minApiVersion = 6;
|
||||
|
||||
// Endpoint for requesting an API token
|
||||
static const _URL_GET_TOKEN = "user/token/";
|
||||
static const _URL_GET_VERSION = "";
|
||||
|
||||
static const _URL_GET_ROLES = "user/roles/";
|
||||
|
||||
@ -518,16 +517,18 @@ class InvenTreeAPI {
|
||||
* Upload a file to the given URL
|
||||
*/
|
||||
Future<http.StreamedResponse> uploadFile(String url, File f,
|
||||
{String name = "attachment", Map<String, String> fields}) async {
|
||||
{String name = "attachment", String method="POST", Map<String, String> fields}) async {
|
||||
var _url = makeApiUrl(url);
|
||||
|
||||
var request = http.MultipartRequest('POST', Uri.parse(_url));
|
||||
var request = http.MultipartRequest(method, Uri.parse(_url));
|
||||
|
||||
request.headers.addAll(defaultHeaders());
|
||||
|
||||
fields.forEach((String key, String value) {
|
||||
request.fields[key] = value;
|
||||
});
|
||||
if (fields != null) {
|
||||
fields.forEach((String key, String value) {
|
||||
request.fields[key] = value;
|
||||
});
|
||||
}
|
||||
|
||||
var _file = await http.MultipartFile.fromPath(name, f.path);
|
||||
|
||||
|
@ -301,8 +301,26 @@ class InvenTreePart extends InvenTreeModel {
|
||||
return img.isNotEmpty ? img : InvenTreeAPI.staticThumb;
|
||||
}
|
||||
|
||||
void uploadImage(File image) async {
|
||||
// TODO
|
||||
Future<bool> uploadImage(File image) async {
|
||||
// Upload file against this part
|
||||
final http.StreamedResponse response = await InvenTreeAPI().uploadFile(
|
||||
url,
|
||||
image,
|
||||
method: 'PATCH',
|
||||
name: 'image',
|
||||
);
|
||||
|
||||
if (response == null) {
|
||||
print("uploadImage returned null at '${url}'");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
print("uploadImage returned ${response.statusCode} at '${url}'");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Return the "starred" status of this part
|
||||
|
Loading…
x
Reference in New Issue
Block a user