?:0: attempt to perform arithmetic on field 'fileSize' (a nil value) - lua

How to resolved error or possible ways to resolved it?
Guys, i've developed plugin using Lua language which can be integrate or run from Adobe's LightRoom Classic. Currently i need to upload or send a file to server but i can not. Everytime i called the POST API which is multipart/form-data error popup "?:0: attempt to perform arithmetic on field 'fileSize' (a nil value)". Not even API is being called this error pops up before API call. after debug I can assure the possible issue is in creating mimeChunks with file type.
I have developed the code like below, can any one help me out with suggestions so that i can able to resolved issue?
local filePath = assert("C:\Users\Ankit\Desktop\Hangman.PNG")
local fileName = LrPathUtils.leafName(filePath)
local mimeChunks = {}
mimeChunks[#mimeChunks + 1] = {
name = 'api_sig',
value = "test value"
}
mimeChunks[#mimeChunks + 1] = {
name = "file",
filePath = filePath,
fileName = fileName,
contentType = "application/octet-stream"
}
local postUrl = "API endpoint"
local result, hdrs = LrHttp.postMultipart(postUrl, mimeChunks)
if result then
LrDialogs.message("Form Values", result)
else
LrDialogs.message("Form Values", "API issue")
end

Eventually image or file path itself cause the issue, there are no such indications or articles related to this functionality, but yes "add-on backslash" will work out for sure. Kindly review the below code for more detailed bifurcation which pass dynamic selected file or image path.
local function uploadFile(filePath)
local fileName = LrPathUtils.leafName( filePath )
local mimeChunks = {}
mimeChunks[ #mimeChunks + 1 ] = { name = 'api_sig', value = "test value"}
mimeChunks[#mimeChunks + 1] = {
name = "file",
filePath = filePath,
fileName = fileName,
contentType = "image/jpeg" --multipart/form-data --application/octet-stream
}
import "LrTasks".startAsyncTask(
function()
local postUrl = "http://cms.local.com/api/v1/upload"
local result, hdrs = LrHttp.postMultipart(postUrl, mimeChunks)
if result then
LrDialogs.message("Image uploaded.", result)
else
LrDialogs.message("Error", "API issue")
end
end
)
end
Above uploadFile method will automatically call the API and post form-data collection. Below code is for call uploadFile function which select all the images from catalog.
for p, photo in ipairs(LrApplication.activeCatalog()) do
uploadFile(assert(photo:getRawMetadata('path')));
end
Above code will help you out the selection of categlog with Adobe's LightRoom Plugin.

Related

Lua remove folders in file path from request uri

I have URL's like this:
/path1/path2/path3/path4/path5/96_6.txt
/path1/path2/path3/path4/path5/96_7.txt?blah=1
So far I am doing the following to obtain the file on the end of my URL:
local request_uri = "/path1/path2/path3/path4/path5/96_645.txt?lol=1"
local name = request_uri:match( "([^/]+)$" )
local filename = string.gsub(name, "?.*", "")
print(name)
print(filename)
What outputs:
96_645.txt?lol=1
96_645.txt
What I want to do is to remove path2 and path3 from my URL. The issue is they are dynamic folder paths and can contain characters.
What is the best solution for this?
Try this one:
function fix_url(p)
p, _ = string.gsub(p, '^/([^/]+)/[^/]+/[^/]+/(.*)', '/%1/%2')
return p
end
Here are some tests:
p = fix_url('/path1/path2/path3/path4/path5/96_6.txt')
assert(p == '/path1/path4/path5/96_6.txt')
p = fix_url('/path1/path2/path3/path4/path5/96_7.txt?blah=1')
assert(p == '/path1/path4/path5/96_7.txt?blah=1')
p = fix_url('/path1/foo.txt')
assert(p == '/path1/foo.txt')

Springdoc-openapi external json file with relative path for example request body

I am looking for a way to use a relative path for external JSON files while defining the example request body.
My current definition is like this:
#Operation(requestBody = #RequestBody(description = "Request", content = #Content(schema = #Schema(implementation = Request.class), examples = {
#ExampleObject(
name = "An example request",
value = "{\n" +
"\"token\":\"token\"\n" +
"}",
summary = "Request"
)}
)),summary = "summ", description = "desc")
I want to keep JSON value on an external JSON file.
I know that "externalValue" field can be used instead of "value" to reference an external json file, but it requires a URL. I want to keep it in the project file and use the relative path. I am using Open API v3.
For those who are just looking for external JSON file usage, you can use external JSON file from a URL like this:
#Operation(requestBody = #RequestBody(description = "Request", content = #Content(schema = #Schema(implementation = Request.class), examples = {
#ExampleObject(
name = "An example request",
externalValue = "http://domain/test/example.json",
summary = "Request"
)}
)),summary = "summ", description = "desc")

how to pass additional parametes to spider parse function in scrapy during web scraping

I am trying to pass additional information to the parse function but it is giving a type error.
TypeError: parse() got an unexpected keyword argument 'body'
i am unable to resolve this issue.
"""
return [scrapy.Request(url=website.search_url.format(prod), callback=self.parse,
cb_kwargs = {"body":website.body_xpath,"product_list":website.products_list_xpath,
"names":website.products_name_xpath,"selling_price":website.selling_price_xpath,
"market_price":website.market_price_xpath}) for website in websites for prod in modified_products]
def parse(self, response):
body = response.cb_kwargs.get("body")
product_list = response.cb_kwargs.get("product_list")
name = response.cb_kwargs.get("names")
selling_price = response.cb_kwargs.get("selling_price")
market_price = response.cb_kwargs.get("market_price")
"""
I forgot to write those names in parse function definition, after adding them i am getting the correct result. Thanks for having a look at it.
"""
return [scrapy.Request(url=website.search_url.format(prod), callback=self.parse,
cb_kwargs = dict(body = website.body_xpath, product_list = website.products_list_xpath,
name = website.products_name_xpath, selling_price = website.selling_price_xpath,
market_price = website.market_price_xpath)) for website in websites for prod in modified_products]
def parse(self, response, body, product_list, name, selling_price, market_price):
body = response.cb_kwargs["body"]
product_list = response.cb_kwargs["product_list"]
name_ = response.cb_kwargs["name"]
selling_price_ = response.cb_kwargs["selling_price"]
market_price_ = response.cb_kwargs["market_price"]
"""

Capture full response in Lua Socket call

I am trying to call a REST API through LUA. However, I am not able to capture full raw response returned by the API. Below is the code sample:
local http_socket = require("socket.http")
local pretty_print = require("pl.pretty")
local header = {
["x-device-type"] = "M",
["authorization"] = "ashdjkashd",
["x-app-secret"] = "asdasda",
["x-user-id"] = "asdasdasd"
}
r, c, h = http_socket.request {
method = "GET", -- Validation API Method
url = "http://google.com", -- Validation API URL
headers = header
}
print(r .. c)
pretty_print.dump(h)
I'm using lua 5.3, and luarocks version=2.4.1.
In variable c i am getting code, and in h there are a few headers. I need to capture full response returned by the API.
As you may know, luasocket's http.request supports two forms of usage. I'm assuming you need the second form to customize the resty request for that particular API.
In this case to capture the response body you'll need to use the sink field with ltn12.sink module. For example
local ltn12 = require 'ltn12'
-- ...
local res = {}
r, c, h, s = http_socket.request
{
method = "GET", -- Validation API Method
url = "http://google.com", -- Validation API URL
headers = header,
sink = ltn12.sink.table(res)
}
res = table.concat(res)
print(res)
The table.concat is needed since the response could be comprised of multiple chunk sizes(appended to res as it's received).
You can also write it out to file by replacing above with ltn12.sink.file, eg. using ltn12.sink.file(io.stdout) will dump the response to standard output.

vlc lua: how do I get the full path of the current playing item?

I'm not a programmer so this is difficult for me. I want to make an extension to send the full path to the clipboard in the full format. Example:
D:\MyFolder\music\audio.mp3
I recently found and butchered this extension which sends the running time to the clipboard. Is it possible to modify it so it gets the full path instead of the running time?
I'm using VLC media player 2.0.5 Twoflower 32 bits.
Windows 7 professional 32bits SP1
Here's the content of the .lua file I'm using and want to modify:
-- Time2Clip.lua -- VLC extension --
--[[
INSTALLATION:
Put the file in the VLC subdir /lua/extensions, by default:
* Windows (all users): %ProgramFiles%\VideoLAN\VLC\lua\extensions\
Restart the VLC.
Then you simply use the extension by going to the "View" menu and selecting it.
--]]
function descriptor()
return {
title = "Time2Clip";
version = "1.0";
author = "valuex";
url = 'https://forum.videolan.org/viewtopic.php?f=29&t=101114';
shortdesc = "Time2Clip";
description = "<div style=\"background-color:lightgreen;\"><b>just a simple VLC extension </b></div>";
capabilities = {"input-listener"}
}
end
function activate()
create_dialog()
end
function close()
vlc.deactivate()
end
function create_dialog()
w = vlc.dialog("Time2Clip")
--w2 = w:add_button("Save_to_Clip", click_SAVE,2,1,1,1)
click_SAVE()
end
function click_SAVE()
local input = vlc.object.input()
if input then
local curtime=vlc.path()
-- local curtime=vlc.var.get(input, "time")
-- w2:set_text( curtime )
save_to_clipboard(curtime)
end
end
function save_to_clipboard(var)
strCmd = 'echo '..var..' |clip'
os.execute(strCmd)
vlc.deactivate()
end
I read LUA's README.TXT file and found this but I don't know how to use it. Please help me. Thanks in advance.
input.item(): Get the current input item. Input item methods are:
:uri(): Get item's URI.
:name(): Get item's name.
How about:
function descriptor()
return {
title = "URI2Clip";
version = "1.0";
author = "";
url = '';
shortdesc = "URI2Clip";
description = "<div><b>Copy the media URI to the Windows clipboard</b></div>";
}
end
function activate()
local item = vlc.input.item()
local uri = item:uri()
uri = string.gsub(uri, '^file:///', '')
uri = string.gsub(uri, '/', '\\')
strCmd = 'echo '..uri..' |clip'
os.execute(strCmd)
end
URI returns something like file:///c:/users/username/Documents/song.mp3 so I convert that to c:\users\username... format instead. NB. This is only going to work for saved files, it will mangle web addresses.

Resources