mirror of
https://github.com/inventree/inventree-app.git
synced 2025-11-06 08:15:39 +00:00
File upload fix (#712)
* Allow file upload with non-strict https - Create custom client for uploads - Observe the USE_STRICT_HTTPS setting * Formatting * Update release notes * Fix await code
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
- Fixes URL for reporting issues on GitHub
|
- Fixes URL for reporting issues on GitHub
|
||||||
|
- Fix for uploading files against server with self-signed certificates
|
||||||
|
|
||||||
### 0.20.1 - October 2025
|
### 0.20.1 - October 2025
|
||||||
---
|
---
|
||||||
|
|||||||
28
lib/api.dart
28
lib/api.dart
@@ -4,6 +4,7 @@ import "dart:io";
|
|||||||
|
|
||||||
import "package:flutter/foundation.dart";
|
import "package:flutter/foundation.dart";
|
||||||
import "package:http/http.dart" as http;
|
import "package:http/http.dart" as http;
|
||||||
|
import "package:http/io_client.dart";
|
||||||
import "package:intl/intl.dart";
|
import "package:intl/intl.dart";
|
||||||
import "package:inventree/main.dart";
|
import "package:inventree/main.dart";
|
||||||
import "package:one_context/one_context.dart";
|
import "package:one_context/one_context.dart";
|
||||||
@@ -983,12 +984,23 @@ class InvenTreeAPI {
|
|||||||
String method = "POST",
|
String method = "POST",
|
||||||
Map<String, dynamic>? fields,
|
Map<String, dynamic>? fields,
|
||||||
}) async {
|
}) async {
|
||||||
var _url = makeApiUrl(url);
|
bool strictHttps = await InvenTreeSettingsManager().getBool(
|
||||||
|
INV_STRICT_HTTPS,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
var request = http.MultipartRequest(method, Uri.parse(_url));
|
// Create an IOClient wrapper for sending the MultipartRequest
|
||||||
|
final ioClient = IOClient(createClient(url, strictHttps: strictHttps));
|
||||||
|
|
||||||
request.headers.addAll(defaultHeaders());
|
final uri = Uri.parse(makeApiUrl(url));
|
||||||
|
final request = http.MultipartRequest(method, uri);
|
||||||
|
|
||||||
|
// Default headers
|
||||||
|
defaultHeaders().forEach((key, value) {
|
||||||
|
request.headers[key] = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Optional fields
|
||||||
if (fields != null) {
|
if (fields != null) {
|
||||||
fields.forEach((String key, dynamic value) {
|
fields.forEach((String key, dynamic value) {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
@@ -999,20 +1011,24 @@ class InvenTreeAPI {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add file to upload
|
||||||
var _file = await http.MultipartFile.fromPath(name, f.path);
|
var _file = await http.MultipartFile.fromPath(name, f.path);
|
||||||
|
|
||||||
request.files.add(_file);
|
request.files.add(_file);
|
||||||
|
|
||||||
|
// Construct a response object to return
|
||||||
APIResponse response = APIResponse(url: url, method: method);
|
APIResponse response = APIResponse(url: url, method: method);
|
||||||
|
|
||||||
String jsondata = "";
|
String jsondata = "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var httpResponse = await request.send().timeout(Duration(seconds: 120));
|
var streamedResponse = await ioClient
|
||||||
|
.send(request)
|
||||||
|
.timeout(Duration(seconds: 120));
|
||||||
|
final httpResponse = await http.Response.fromStream(streamedResponse);
|
||||||
|
|
||||||
response.statusCode = httpResponse.statusCode;
|
response.statusCode = httpResponse.statusCode;
|
||||||
|
|
||||||
jsondata = await httpResponse.stream.bytesToString();
|
jsondata = httpResponse.body;
|
||||||
|
|
||||||
response.data = json.decode(jsondata);
|
response.data = json.decode(jsondata);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user