diff --git a/lib/api.dart b/lib/api.dart index 67f3b059..1d062680 100644 --- a/lib/api.dart +++ b/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 uploadFile(String url, File f, - {String name = "attachment", Map fields}) async { + {String name = "attachment", String method="POST", Map 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); diff --git a/lib/inventree/part.dart b/lib/inventree/part.dart index ea71051f..82c44a8b 100644 --- a/lib/inventree/part.dart +++ b/lib/inventree/part.dart @@ -301,8 +301,26 @@ class InvenTreePart extends InvenTreeModel { return img.isNotEmpty ? img : InvenTreeAPI.staticThumb; } - void uploadImage(File image) async { - // TODO + Future 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