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

Bugfix/bug57 #59

Open
wants to merge 2 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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 2.0.1-dev.3

- Fixed bug [#57](https://github.com/abdallah-odeh/flutter_file_downloader/issues/57) where images & pdfs might be downloaded without an extension resulting to inability to open the file

## 2.0.1-dev.2

- Fixed bug [#53](https://github.com/abdallah-odeh/flutter_file_downloader/issues/53) endless loop in the background
- Wrapped callbacks with try & catch to avoid app crashes [#54](https://github.com/abdallah-odeh/flutter_file_downloader/issues/54)

## 2.0.1-dev.1

- Fixed bug when using both plugins `flutter_file_downloader` & `geolocator` [#52](https://github.com/abdallah-odeh/flutter_file_downloader/issues/52)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ First, make sure that you've added the permissions to your AndroidManifest.xml
```

Add the following line to your pubspec.yaml
``` flutter_file_downloader: ^2.0.1-dev.1```
``` flutter_file_downloader: ^2.0.1-dev.3```

Next,
add the library import to your dart file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public void startDownload() {
protected abstract void download();

public String getFileNameFromContent(final String contentDisposition) {
if (TextUtils.isEmpty(contentDisposition)) return null;
try {
if (TextUtils.isEmpty(contentDisposition)) return null;
final String[] parts = contentDisposition.split(" ");
return parts[1].replaceFirst("filename=", "");
} catch (Exception e) {
Expand All @@ -58,6 +58,18 @@ public String getFileNameFromContent(final String contentDisposition) {
}
}

public String getFileExtension(final String contentType) {
System.out.println("Getting extension from " + contentType);
if (TextUtils.isEmpty(contentType)) return null;
try {
final String[] parts = contentType.split("/");
return parts[parts.length - 1].toString().toLowerCase();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public String getFileName() {
final Uri uri = Uri.parse(url);
final List<String> path = uri.getPathSegments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,15 @@ public void run() {
if (TextUtils.isEmpty(fileName)) {
fileName = FILE_NAME;
}
final String extension = getFileExtension(urlConnection.getHeaderField("Content-Type"));

BufferedInputStream in = new BufferedInputStream(urlConnection.getInputStream());
File tmpFile = new File(fileName + ".tmp");
final String filePath = fileStoreHandler.createFile(
downloadDestination.getDirectoryPath().getAbsolutePath(),
downloadDestination.subPath,
fileName);
fileName,
extension);
String finalFileName = fileName;
activity.runOnUiThread(() -> {
callbacks.onIDReceived(Calendar.getInstance().getTimeInMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public File writeFile(
callbacks.onProgress(fileName, 0);

try {
file = new File(createFile(directory, fileName));
file = new File(createFile(directory, name, extension));
callbacks.onProgress(fileName, 20);
fileOutputStream = new FileOutputStream(file);
callbacks.onProgress(fileName, 40);
Expand Down Expand Up @@ -69,37 +69,45 @@ public File writeFile(
return file;
}

public String createFile(String directory, String subPath, String name) throws IOException {
public String createFile(String directory, String subPath, String name, final String extension) throws IOException {
if (TextUtils.isEmpty(subPath)) {
return createFile(
directory,
name);
name,
extension);
}
subPath = FileUtils.fixSubPath(subPath);
return createFile(
String.format("%s/%s", directory, subPath),
name);
name,
extension);
}

public String createFile(final String directory, final String name) throws IOException {
public String createFile(final String directory, final String name, final String extension) throws IOException {
String path = String.format("%s/%s", directory, name);
final String[] splitted = name.split("\\.");
final String extension = splitted[splitted.length - 1];
final String extension2 = splitted[splitted.length - 1];
final String fileName = name.replaceAll("." + extension, "").replaceAll("." + extension2, "");
if (!FileUtils.createDir(directory)) {
PluginLogger.log("Create directories " + directory + " failed!");
return null;
}
int instanceNo = 0;
String ext = extension;
if (TextUtils.isEmpty(extension)) ext = extension2;
if (TextUtils.isEmpty(extension2)) {
PluginLogger.log("Could not detect file extension for file " + name);
}
do {
File file = new File(path);
if (file.exists()) {
instanceNo++;
path = String.format(Locale.ENGLISH,
"%s/%s-%d.%s",
directory,
name.replaceAll("." + extension, ""),
fileName.replaceAll("." + ext, ""),
instanceNo,
extension
ext
);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.0.1-dev.1"
version: "2.0.1-dev.3"
flutter_lints:
dependency: "direct dev"
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_file_downloader
description: A simple flutter plugin that downloads any file type to downloads directory
version: 2.0.1-dev.1
version: 2.0.1-dev.3
issue_tracker: https://github.com/abdallah-odeh/flutter_files_downloader/issues
homepage: https://github.com/abdallah-odeh/flutter_files_downloader

Expand Down