Skip to content

Commit

Permalink
Support for data flavors in file writes (qzind#917)
Browse files Browse the repository at this point in the history
Support for data flavors in file writes
  • Loading branch information
Brett Berenz authored Feb 8, 2022
1 parent 2a7c6d4 commit f6837af
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions js/qz-tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,7 @@ var qz = (function() {
* @param {boolean} [params.sandbox=true] If relative location from root is only available to the certificate's connection, otherwise all connections
* @param {boolean} [params.shared=true] If relative location from root is accessible to all users on the system, otherwise just the current user
* @param {boolean} [params.append=false] Appends to the end of the file if set, otherwise overwrites existing contents
* @param {string} [params.flavor='plain'] Flavor of data format used, valid flavors are <code>[base64 | file | hex | plain]</code>.
* @returns {Promise<null|Error>}
*
* @memberof qz.file
Expand Down
23 changes: 23 additions & 0 deletions sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,27 @@ <h4 class="panel-title">Options</h4>
<label for="fileData">Data To Write</label>
<textarea id="fileData" rows="5" style="resize:vertical" class="form-control"></textarea>
</div>
<div class="form-group">
<label>Data Type</label>
<div>
<label>
<input type="radio" name="fileFlavor" id="fileFlavorPLN" value="plain" />
Plain
</label>
<label>
<input type="radio" name="fileFlavor" id="fileFlavorB64" value="base64" />
Base64
</label>
<label>
<input type="radio" name="fileFlavor" id="fileFlavorURL" value="file" />
File
</label>
<label>
<input type="radio" name="fileFlavor" id="fileFlavorHEX" value="hex" />
Hexadecimal
</label>
</div>
</div>
<div class="form-group">
<label for="includePattern" class="tip" data-toggle="tooltip" title="File pattern to match when listening for file events (e.g. *.txt)">
Include File Pattern
Expand Down Expand Up @@ -2341,6 +2362,7 @@ <h4 class="panel-title">Options</h4>
sandbox: $("#fileSandbox").prop("checked"),
shared: $("#fileShared").prop("checked"),
append: $("#fileAppend").prop('checked'),
flavor: $("input[name='fileFlavor']:checked").val(),
data: $("#fileData").val()
};

Expand Down Expand Up @@ -2552,6 +2574,7 @@ <h4 class="panel-title">Options</h4>
function resetFileOptions() {
$("#fileLocation").val('');
$("#fileData").val('');
$("#fileFlavorPLN").prop('checked', true);
$("#fileShared").prop('checked', true);
$("#fileSandbox").prop('checked', true);
$("#fileAppend").prop('checked', false);
Expand Down
33 changes: 29 additions & 4 deletions src/qz/communication/FileParams.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package qz.communication;

import org.apache.commons.ssl.Base64;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import qz.utils.ByteUtilities;
import qz.utils.FileUtilities;

import java.io.IOException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Locale;

/**
* Created by Kyle on 2/28/2018.
*/
public class FileParams {

public enum Flavor {
BASE64, FILE, HEX, PLAIN
}

private Path path;
private byte[] data;
private String data;
private Flavor flavor;

private boolean shared;
private boolean sandbox;
Expand All @@ -24,7 +34,8 @@ public class FileParams {

public FileParams(JSONObject params) throws JSONException {
path = Paths.get(params.getString("path"));
data = params.optString("data", "").getBytes();
data = params.optString("data", "");
flavor = Flavor.valueOf(params.optString("flavor", "PLAIN").toUpperCase(Locale.ENGLISH));

shared = params.optBoolean("shared", true);
sandbox = params.optBoolean("sandbox", true);
Expand All @@ -36,8 +47,22 @@ public Path getPath() {
return path;
}

public byte[] getData() {
return data;
public byte[] getData() throws IOException {
switch(flavor) {
case BASE64:
return Base64.decodeBase64(data);
case FILE:
return FileUtilities.readRawFile(data);
case HEX:
return ByteUtilities.hexStringToByteArray(data.trim());
case PLAIN:
default:
return data.getBytes();
}
}

public Flavor getFlavor() {
return flavor;
}

public boolean isShared() {
Expand Down

0 comments on commit f6837af

Please sign in to comment.