Corrupt file when using Azure Functions External File binding - encoding

I'm running a very simple ExternalFileTrigger scenario in Azure Functions were I copy one created image file from one onedrive directory to another.
function.json
{
"bindings": [
{
"type": "apiHubFileTrigger",
"name": "input",
"direction": "in",
"path": "Bilder/Org/{name}",
"connection": "onedrive_ONEDRIVE"
},
{
"type": "apiHubFile",
"name": "$return",
"direction": "out",
"path": "Bilder/Minimized/{name}",
"connection": "onedrive_ONEDRIVE"
}
],
"disabled": false
}
run.csx
using System;
public static string Run(string input, string name, TraceWriter log)
{
log.Info($"C# File trigger function processed: {name}");
return input;
}
Every things seems to work well BUT the new output image file i corrupt. The size is almost twice as big.
When looking at the encoding the original file is in ANSI but the new generated file from Azure Functions is in UTF-8.
It's working fine when I'm using a text file when source encoding is UTF-8.
Is it possible to force Azure binding ExternalFileTrigger to use ANSI? Or how to solve this?

UPDATE 2019: The external file binding seems to be deprecated from the current version of Azure Functions.
If you want to copy the file as-is, or do more fine-grained binary operations on the file contents, I recommend using Stream type instead of string for your input and output bindings:
public static async Task Run(Stream input, Stream output, string name, TraceWriter log)
{
using (MemoryStream ms = new MemoryStream())
{
input.CopyTo(ms);
var byteArray = ms.ToArray();
await output.WriteAsync(byteArray, 0, byteArray.Length);
}
log.Info($"C# File trigger function processed: {name}");
}
Change the ouput binding in function.json:
"name": "output",
This function will do exact binary copy of the file, without conversion.
You can see which other types you can use for bindings in External File bindings (see "usage" sections).

Related

VSCode extension for quick semantic info

I am used to point the mouse and get information about certain references in Visual studio code.
Here one example, using Javascript, I point the mouse to a function reference and I get information about the function signature.
I would like to have something similar to other files.
e.g.
Take the following example, in a less popular language
module top #(
parameter NB=4
);
logic [NB /*I would like info here */ -1:0] b;
endmodule
How can I write an extension that when I point the mouse to the parameter it shows me the the declaration in a box, preferably with the same syntax highlight as it is shown in the editor.
Now there is a pull request with a sample to vscode-extension-samples.
Basically you have to write something like this
import * as vscode from 'vscode';
class SimpleHoverProvider implements vscode.HoverProvider {
public provideHover(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
): vscode.Hover | null {
return new vscode.Hover(`${location.line}: ${location.character}`);
// return null; if there is no information to show
}
}
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, hover-provider-sample extension is active!');
const hoverProvider = new SimpleHoverProvider();
vscode.languages.registerHoverProvider('text', hoverProvider);
}
And define languages and activation events on package.json
{
"activationEvents": [
"onLanguage:text",
"onLanguage:report"
],
"contributes": {
"languages": [
{
"id": "text",
"extensions": [
".txt"
]
},
{
"id": "report",
"extensions": [
".rpt"
]
}
]
},
}

Adding jar to vscode debug extension

We are having a runnable jar for the DAP(Debugger Adapater Protocol) of ABL language and we are planning to add a vscode extension for that ABL DAP.
Could you provide us some documentation or give us an idea about how to do it?
There are basically two approaches for making VS Code run your *.jar:
Declarative:
In the package.json of your extension, you can register your executable as part of the debuggers extension point. Here is the doc: https://code.visualstudio.com/api/extension-guides/debugger-extension#anatomy-of-the-package.json-of-a-debugger-extension.
And here is an example from the node.js debugger.
Basically the following attributes are available:
"runtime": "node",
"runtimeArgs": [ "--arguments_passed_to_runtime" ],
"program": "./dist/dap.js",
"args": [ "--arguments_passed_to_program" ],
The resulting command that VS Code will call is this:
node --arguments_passed_to_runtime ./dist/dap.js --arguments_passed_to_program
You do not have to make a distinction between runtime and program. You can use either "runtime" or "program" and leave out the other.
Non-declarative:
Alternatively you can use extension code to "calculate" the command that VS Code should execute when launching your Debug Adapter. See this API: https://github.com/microsoft/vscode/blob/4c3769071459718f89bd48fa3b6e806c83cf3336/src/vs/vscode.d.ts#L8797-L8816
Based on this API the following snippet shows how you could create a DebugAdapterDescriptor for your abl debugger:
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: ExtensionContext) {
vscode.debug.registerDebugAdapterDescriptorFactory('abl', {
createDebugAdapterDescriptor(session: DebugSession, executable: DebugAdapterExecutable | undefined): ProviderResult<DebugAdapterDescriptor> {
return new vscode.DebugAdapterExecutable(
"/usr/bin/java",
[
join(context.extensionPath, "bin", "abl.jar")
],
{
"cwd": "some current working directory path",
"env": {
"key": "value"
}
}
);
}
});
}

Post attachment in Service Now

I'm in a quandary on how to get this working.
In Postman, I can upload an attachment without any issue.
I'm uploading a simple text file.
The code from postmanshows this:
var form = new FormData();
form.append("uploadFile", "C:\\temp\\test.txt");
var settings = {
"async": true,
"crossDomain": true,
"url": "https://xxxxxxx.service-now.com/api/now/attachment/file?table_name=problem&table_sys_id=oiui5346jh356kj3h634j6hk&file_name=Toast",
"method": "POST",
"headers": {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==",
"Cache-Control": "no-cache",
"Postman-Token": "39043b7f-8b2c-1dbc-6a52-10abd25e91c1"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
When I use this on an .asp page I get a 400 error and a response from the console that says:
"Failed to create the attachment. File part might be missing in the request."
How do you get the file you want attach into the code correctly. I thought hard coding it in would have worked. How do you get the code to find the file on the local users pc. Once I get this working I eventually want to have a file input button to select the file.
Thanks,
Scott
Your code looks fine except this line:
form.append("uploadFile", "C:\\temp\\test.txt");
Passing the file name as the second parameter won't work, according to the documentation of FormData.append here, you need to pass some blob/file object pointing to the document it self (not a string)
Now there are 2 possible scenarios:
Scenario 1
The user selects the file manually using a browse button
Here you need to add the input to your page and a trigger to upload the file when it's selected, something like below maybe:
uploadDataFile();
function uploadDataFile(fileInput) {
// creates the FormData object
var form = new FormData();
// get the first file in the file list of the input
// (you will need a loop instead if the user will select many files)
form.append("uploadFile", fileInput.files[0]);
// ... the rest of your AJAX code here ...
}
<input type="file" onchange="uploadDataFile(this)" />
Scenario 2
Uploading the file directly without user intervention
Here you need to construct the file object manually same as in this answer and then you will add it normally to your data object
function uploadDataFile() {
// creates the file object
var fileObject = new File (...);
// creates a data object and appends the file object to it
var form = new FormData();
form.append("uploadFile", fileObject);
// ... the rest of your AJAX code here ...
}
One final note
Please pay attention to the browser compatibility for FormData & File objects

Not able to insert the base64 data into the Word with Office Word add-in

I am trying to insert file which is formatted as Base64 using insertFileFromBase64() but I am getting this exception:
{
"name": "OfficeExtension.Error",
"code": "GeneralException",
"message": "ooxmlIsMalformated",
"traceMessages": [],
"innerError": null,
"debugInfo": {
"code": "GeneralException",
"message": "ooxmlIsMalformated",
"errorLocation": "Body.insertFileFromBase64"
}
}
My code is:
// documentData - is a base64 string.
// Encoded base64 string = "V29yZCBEb2N1bWVudCBpcyBub3QgZ2V0IGluc2VydGVkIHVzaW5nIGluc2VydEZpbGVCYXNlNjQ="
body.insertFileFromBase64(documentData,window.Word.InsertLocation.replace);
I have tried other methods for HTML and text that are working, but getting the exception with insertFileFromBase64.
Can someone please guide me if I am missing anything here?
From the documentation, the base64File parameter takes "The base64 encoded content of a .docx file."
The string your passing isn't an encoded .docx, it is simply an encoded string. The exception your getting is correct, the OOXML you are passing is malformated (or more precisely, it isn't OOXML at all).
You will need to pass in a proper OOXML document that has been Base64 encoded for this method to work.

Replace audio on video with Azure Media Encoder

I'm using Azure Media services to encode uploaded videos for streaming. I would like to replace the audio track with a different audio source. I'm already creating a custom preset configuration to the encoding but I haven't found a way to replace or overlay different audio. Is this possible?
First of all, I presume you are attempting to use Media Encoder Standard, and not the deprecated Azure Media Encoder.
Is the replacement audio source in sync/aligned in time with respect to the video - same starting timestamp, same duration etc?
While the overlay feature will not work in this case (it'll end up mixing the audio from the original with the replacement), you can attempt a simple workaround which requires the content to be time-aligned. I'll be able to share sample code later today.
The workaround below has three sections:
Describes the preset that does video-only encoding of the video file
Describes the preset that does audio-only encoding of the overlay/replacement file
The code block that shows how to submit a Job with two Tasks that write to the same output Asset
Video Only Encoding Preset
Save the JSON below to a suitable file, say "C:\TEMP\VideoOnly.json". I'll use a single bitrate setting as an example to keep the JSON brief
{
"Version": 1.0,
"Codecs": [
{
"KeyFrameInterval": "00:00:02",
"H264Layers": [
{
"Profile": "Auto",
"Level": "auto",
"Bitrate": 2500,
"MaxBitrate": 2500,
"BufferWindow": "00:00:05",
"Width": 1280,
"Height": 720,
"BFrames": 3,
"Type": "H264Layer",
"FrameRate": "0/1"
}
],
"Type": "H264Video"
}
],
"Outputs": [
{
"FileName": "{Basename}_{Resolution}_{Bitrate}.mp4",
"Format": {
"Type": "MP4Format"
}
}
]
}
Audio Only Encoding Preset
Save the JSON below to a suitable file, say "C:\TEMP\AudioOnly.json".
{
"Version": 1.0,
"Codecs": [
{
"Profile": "AACLC",
"Channels": 2,
"SamplingRate": 48000,
"Bitrate": 128,
"Type": "AACAudio"
}
],
"Outputs": [
{
"FileName": "{Basename}_AAC_{AudioBitrate}.mp4",
"Format": {
"Type": "MP4Format"
}
}
]
}
Encoding
The code below assumes that you have the video file uploaded as an Asset myVideoAsset and the audio file uploaded as an Asset myAudioAsset.
string videoConfig = File.ReadAllText(_presetFiles + #"C:\TEMP\VideoOnly.json");
string audioConfig = File.ReadAllText(_presetFiles + #"C:\TEMP\AudioOnly.json");
// Prepare a job with two Tasks that write to the same Asset
IJob job = _context.Jobs.Create(#"Encoding " + myVideoAsset.Name + #" and " + myAudioAsset.Name);
IMediaProcessor mediaProcessor = GetLatestMediaProcessorByName("Media Encoder Standard");
ITask videoTask = job.Tasks.AddNew("Video Task", mediaProcessor, videoConfig, TaskOptions.DoNotCancelOnJobFailure | TaskOptions.DoNotDeleteOutputAssetOnFailure);
videoTask.InputAssets.Add(myVideoAsset);
IAsset outputAsset = videoTask.OutputAssets.AddNew(myVideoAsset.Name + #" plus " + myAudioAsset.Name + # - Encoded", options: AssetCreationOptions.None, formatOption: AssetFormatOption.None);
ITask audioTask = job.Tasks.AddNew("Audio Task", mediaProcessor, audioConfig, TaskOptions.DoNotCancelOnJobFailure | TaskOptions.DoNotDeleteOutputAssetOnFailure);
audioTask.InputAssets.Add(myAudioAsset);
audioTask.OutputAssets.Add(outputAsset); // Note the re-use of outputAsset here
Console.WriteLine("Submitting transcoding job...");
job.Submit();
// Wait for job to succeed, etc.