Skip to content

Commit

Permalink
feat: add fileName
Browse files Browse the repository at this point in the history
  • Loading branch information
gevgasparyan committed Oct 23, 2020
1 parent 5230ead commit 8c50a7b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ RNQRGenerator.generate({
bottom: 20,
right: 20,
},
fileName: 'My Profile', // (optional), name of the image file to store in FileSystem.
correctionLevel: 'L', // default is 'H', also available 'M' and 'Q'.
})
.then(response => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public String getName() {
@ReactMethod
public void generate(final ReadableMap options, @Nullable Callback failureCallback, @Nullable Callback successCallback) {
String value = options.hasKey("value") ? options.getString("value") : "";
String fileName = options.hasKey("fileName") ? options.getString("fileName") : null;
String correctionLevel = options.hasKey("correctionLevel") ? options.getString("correctionLevel") : "H";
Double width = options.hasKey("width") ? options.getDouble("width") : 100;
Double height = options.hasKey("height") ? options.getDouble("height") : 100;
Expand Down Expand Up @@ -98,7 +99,7 @@ public void generate(final ReadableMap options, @Nullable Callback failureCallba

try {
File cacheDirectory = this.reactContext.getCacheDir();
File imageFile = new File(getOutputFilePath(cacheDirectory, ".png"));
File imageFile = new File(getOutputFilePath(cacheDirectory, fileName, ".png"));
imageFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(imageFile);

Expand Down Expand Up @@ -233,10 +234,10 @@ public static File ensureDirExists(File dir) throws IOException {
return dir;
}

public static String getOutputFilePath(File directory, String extension) throws IOException {
public static String getOutputFilePath(File directory, String fileName, String extension) throws IOException {
ensureDirExists(directory);
String filename = UUID.randomUUID().toString();
return directory + File.separator + filename + extension;
String name = (fileName != null) ? fileName : UUID.randomUUID().toString();
return directory + File.separator + name + extension;
}

public Bitmap getBitmapFromSource(String path, String base64) throws IOException {
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type QRCodeGenerateOptions = {
height?: number,
base64?: boolean,
padding?: Padding,
fileName?: string,
correctionLevel: 'L' | 'M' | 'Q' | 'H',
};

Expand Down
8 changes: 5 additions & 3 deletions ios/RNQrGenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ - (dispatch_queue_t)methodQueue

NSString *qrData = [RCTConvert NSString:options[@"value"]];
NSString *level = [RCTConvert NSString:options[@"correctionLevel"]];
NSString *fileName = [RCTConvert NSString:options[@"fileName"]];
level = [self getCorrectionLevel:level];
float width = [RCTConvert float:options[@"width"]];
float height = [RCTConvert float:options[@"height"]];
Expand Down Expand Up @@ -88,7 +89,7 @@ - (dispatch_queue_t)methodQueue
NSData *qrData = UIImagePNGRepresentation(image);

NSString *directory = [[self cacheDirectoryPath] stringByAppendingPathComponent:@"QRCode"];
NSString *path = [self generatePathInDirectory:directory withExtension:@".png"];
NSString *path = [self generatePathInDirectory:directory fileName:fileName withExtension:@".png"];
response[@"uri"] = [self writeImage:qrData toPath:path];

response[@"width"] = @(image.size.width);
Expand Down Expand Up @@ -162,9 +163,10 @@ - (dispatch_queue_t)methodQueue
}
}

- (NSString *)generatePathInDirectory:(NSString *)directory withExtension:(NSString *)extension
- (NSString *)generatePathInDirectory:(NSString *)directory fileName:(NSString *)name withExtension:(NSString *)extension
{
NSString *fileName = [[[NSUUID UUID] UUIDString] stringByAppendingString:extension];
NSString *fileName = name ? name : [[NSUUID UUID] UUIDString];
fileName = [fileName stringByAppendingString:extension];
[self ensureDirExistsWithPath:directory];
return [directory stringByAppendingPathComponent:fileName];
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rn-qr-generator",
"version": "1.1.2",
"version": "1.1.3",
"description": "React native QR Code generator",
"main": "index.js",
"types": "typings/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface QRCodeGenerateOptions {
height?: number;
base64?: boolean;
padding?: Padding;
fileName?: string,
correctionLevel: 'L' | 'M' | 'Q' | 'H',
};

Expand Down

0 comments on commit 8c50a7b

Please sign in to comment.