Skip to content

Commit

Permalink
Revert "Moves the image validation to native (CarnegieTechnologies#44)…
Browse files Browse the repository at this point in the history
…" (CarnegieTechnologies#53)

This reverts commit 08c6e9f.
  • Loading branch information
jelenalecic authored Apr 3, 2020
1 parent 76fd710 commit 3a27c70
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package carnegietechnologies.gallery_saver
import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import androidx.core.app.ActivityCompat
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
Expand All @@ -25,18 +24,6 @@ class GallerySaver internal constructor(private val activity: Activity) :
private val job = Job()
private val uiScope = CoroutineScope(Dispatchers.Main + job)

/**
* Checks if the file specified by the [path] is an image.
*
* The function returns true/false depending on whether the bounds can be read.
*/
internal fun isImage(path: String): Boolean {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(path, options)
return (options.outWidth != -1 && options.outHeight != -1)
}

/**
* Saves image or video to device
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ class GallerySaverPlugin private constructor(
when (call.method) {
"saveImage" -> gallerySaver.checkPermissionAndSaveFile(call, result, MediaType.image)
"saveVideo" -> gallerySaver.checkPermissionAndSaveFile(call, result, MediaType.video)
"image.check" -> {
val path = call.arguments<String>()
val isImage = gallerySaver.isImage(path)

result.success(isImage)
}
else -> result.notImplemented()
}
}
Expand Down
35 changes: 0 additions & 35 deletions ios/Classes/SwiftGallerySaverPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,11 @@ public class SwiftGallerySaverPlugin: NSObject, FlutterPlugin {
self.saveMedia(call, .image, result)
} else if call.method == "saveVideo" {
self.saveMedia(call, .video, result)
} else if call.method == "image.check" {
checkImage(call, result)
} else {
result(FlutterMethodNotImplemented)
}
}

/// Attempts to read the size of a image specified as the single argument to the `call`.
///
/// - Parameters:
/// - call: method object with params for saving media
/// - result: flutter result that gets sent back to the dart code
///
func checkImage(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
let path = call.arguments as! String
let url = URL(fileURLWithPath: path)
let size = SwiftGallerySaverPlugin.sizeOfImageAt(url: url)

result(size != nil)
}

/// Tries to save image to the photos app.
/// If user hasn't already permitted saving to the photos, it will be requested
/// to do so.
Expand Down Expand Up @@ -138,24 +122,5 @@ public class SwiftGallerySaverPlugin: NSObject, FlutterPlugin {
}
}
}

private static func sizeOfImageAt(url: URL) -> CGSize? {
// with CGImageSource we avoid loading the whole image into memory
guard let source = CGImageSourceCreateWithURL(url as CFURL, nil) else {
return nil
}

let propertiesOptions = [kCGImageSourceShouldCache: false] as CFDictionary
guard let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, propertiesOptions) as? [CFString: Any] else {
return nil
}

if let width = properties[kCGImagePropertyPixelWidth] as? CGFloat,
let height = properties[kCGImagePropertyPixelHeight] as? CGFloat {
return CGSize(width: width, height: height)
} else {
return nil
}
}
}

13 changes: 12 additions & 1 deletion lib/files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ const List<String> videoFormats = [
'.mkv',
'.flv'
];

const List<String> imageFormats = [
'.jpeg',
'.png',
'.jpg',
'.gif',
'.webp',
'.tif',
'.heic'
];
const http = 'http';

bool isLocalFilePath(String path) {
Expand All @@ -19,3 +27,6 @@ bool isLocalFilePath(String path) {

bool isVideo(String path) =>
videoFormats.contains(extension(path).toLowerCase());

bool isImage(String path) =>
imageFormats.contains(extension(path).toLowerCase());
6 changes: 1 addition & 5 deletions lib/gallery_saver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class GallerySaver {
if (path == null || path.isEmpty) {
throw ArgumentError(pleaseProvidePath);
}
if (!await _checkIfFileIsImage(path)) {
if (!isImage(path)) {
throw ArgumentError(fileIsNotImage);
}
if (!isLocalFilePath(path)) {
Expand All @@ -65,10 +65,6 @@ class GallerySaver {
return result;
}

static Future<bool> _checkIfFileIsImage(String path) async {
return _channel.invokeMethod<bool>('image.check', path);
}

static Future<File> _downloadFile(String url) async {
print(url);
http.Client _client = new http.Client();
Expand Down

0 comments on commit 3a27c70

Please sign in to comment.