Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoimport: Fix file extensions from redirects #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:
3 changes: 2 additions & 1 deletion lib/api/preset/autodownload/single/generic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ part of preset;

// twitter: fxtwitter offers a url to give only the image. getting the artist is as easy as reading the first path segment
Future<PresetImage> twitterToPresetImage(Uri uri) async {
// final res = await http.get(Uri.parse(["https://d.fxtwitter.com", uri.path].join()));
// final res = await lbHttp.get(Uri.parse(["https://d.fxtwitter.com", uri.path].join()));

final downloadedFileInfo = await downloadFile(Uri.parse(["https://d.fxtwitter.com", uri.path].join()));
debugPrint(downloadedFileInfo.path);

return PresetImage(
image: downloadedFileInfo,
Expand Down
23 changes: 17 additions & 6 deletions lib/utils/download_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ import 'package:path_provider/path_provider.dart';
typedef HandleChunk = Function(List<int> chunk, http.StreamedResponse response);

int hasDownloaded = 0;
Future<File> downloadFile(Uri uri, {HandleChunk? handleChunk}) async {
Future<File> downloadFile(Uri uri, {HandleChunk? handleChunk, bool followRedirects = true}) async {
http.Request request;
http.StreamedResponse response;
do { // lazy work for redirects; do-while because it'll run once if followRedirects = false
request = http.Request("GET", uri);
request.followRedirects = false;
response = await lbHttp.send(request);
if(response.isRedirect) {
final String? newUrl = response.headers['location'];
if(newUrl != null) uri = Uri.parse(newUrl);
else throw "Redirect does not have 'location' header";
} else break;
} while (followRedirects);

final downloadDir = await getTemporaryDirectory();
final file = File(p.join(downloadDir.path, uri.pathSegments.last));

final request = http.Request("GET", uri);
final response = await lbHttp.send(request);
final sink = file.openWrite();
final name = uri.pathSegments.last;
final file = File(p.join(downloadDir.path, name));
IOSink sink = file.openWrite();

await response.stream.map((chunk) {
if(handleChunk == null) {
Expand Down
Loading