Jump to content

[QUESTION] How do i get a URL`s size.


Recommended Posts

server:

function startImageDownload(player)
    fetchRemote ("https://i.imgur.com/Aa9kXWi.jpeg", sendFiletoClient, "", false, player) -- use direct link for this
end

function sendFiletoClient(responseData, errorCode, player)
    if (errorCode == 0) then
        triggerClientEvent(player, "printImageInfo", resourceRoot, responseData)
    end
end

local player = getPlayerFromName("Burak2346")  -- change with your nick
startImageDownload(player)

client:

addEvent("printImageInfo", true)
addEventHandler( "printImageInfo", resourceRoot,
    function(pixels)
        local image = fileCreate ("image.jpeg")
        fileWrite(image, pixels)
        local fileSize = fileGetSize(image)
        outputDebugString(fileSize)
        fileClose(image)
    end
)

 

Since it calculates the file size in bytes, you can do some math and convert it to the size you want, such as kb, mb.

Edited by Burak5312
  • Like 1
Link to comment
addEvent('clothing:save', true)
addEventHandler('clothing:save', resourceRoot,
	function(values)
		
			if not values.id then
				
				values.id = exports.mysql:query_insert_free("INSERT INTO clothing (skin, url, description, price) VALUES (" .. tonumber(values.skin) .. ", '" .. exports.mysql:escape_string(values.url) .. "', '" .. exports.mysql:escape_string(values.description) .. "', " .. tonumber(values.price) .. ")")
				if values.id then
				triggerClientEvent(client, "printImageInfo", resourceRoot, responseData)










					savedClothing[values.id] = values
					outputChatBox('Clothes added with id ' .. values.id .. '.', client, 0, 255, 0)
				else
					outputChatBox('Unable to add clothes.', client, 255, 0, 0)
				end
			else
			
				local existing = savedClothing[values.id]
				if existing then
					if exports.mysql:query_free('UPDATE clothing SET skin = ' .. tonumber(values.skin) .. ', description = "' .. exports.mysql:escape_string(values.description) .. '", price = ' .. tonumber(values.price) .. ' WHERE id = ' .. tonumber(values.id)) then
						
						outputChatBox('Saved clothing.', client, 0, 255, 0)

						existing.skin = tonumber(values.skin)
						
						existing.description = tostring(values.description)
						existing.price = tonumber(values.price)
					else
						outputChatBox('Clothes couldn\'t be saved.', client, 255, 0, 0)
					end
				else
					outputChatBox('Unable to find clothes?', client, 255, 0, 0)
				end
			end
		
	end, false)

This is the code i made, can you tell me how do i get the size of the URL that i entered inside the GUi? In the client is fileCreate('image.jpg') and the pixels are 0 when i print the filesize.
Client:
 

addEvent("printImageInfo", true)
addEventHandler( "printImageInfo", resourceRoot,
    function(pixels)
        local image = fileCreate ("image.jpeg")
        fileWrite(image, pixels)
        local fileSize = fileGetSize(image)
        outputDebugString(fileSize)
        fileClose(image)
        outputDebugString('File size:'..fileSize)

    end)
local save = guiCreateButton(editing_width - 110, editing_height - 30, 100, 25, 'Save', false, editing_window)
	addEventHandler('onClientGUIClick', save,
		function()
			
			local values = {}
			for k, v in ipairs(desc) do
				local value = guiGetText(v.edit)
				if k == 1 or k == 4 then
					value = tonumber(value)
				end
				values[v[1]] = value
			end
			values.id = editing_item and list[editing_item].id or nil

			triggerServerEvent('clothing:save', resourceRoot, values)

			closeEditingWindow()
		end, false)

 

Link to comment

assuming you are using the edit box first get the link you entered with guiGetText

guiGetText()

then create an event on server side

function startImageDownload(player, url)
    fetchRemote (url, sendFiletoClient, "", false, player)
end

function sendFiletoClient(responseData, errorCode, player)
    if (errorCode == 0) then
        triggerClientEvent(player, "printImageInfo", resourceRoot, responseData)
    else
       outputChatBox("Error Image Failed!", player)
    end
end

addEvent("requestGetImage", true)
addEventHandler("requestGetImage", root,
   function(url)
      startImageDownload(source, url)
   end
)

 

 

For client-side event, print the information before closing the file then close the file

addEvent("printImageInfo", true)
addEventHandler( "printImageInfo", resourceRoot,
    function(pixels)
        local image = fileCreate ("image.jpeg")
        fileWrite(image, pixels)
        local fileSize = fileGetSize(image)
        outputChatBox("File Size: "..fileSize.." Byte")
        fileClose(image)
    end
)

then inside onClientGUIClick event call requestGetImage event with on triggerServerEvent

triggerServerEvent("requestGetImage", localPlayer, link here) -- enter here the link you get with guiGetText

 

then printImageInfo will tell you the size of the image with callback

Edited by Burak5312
Link to comment

I have a problem i`m so sorry... How do i get the guiGetText from this ? Is a weird script.
 

local desc = { { 'skin', 'Base Skin' }, { 'url', 'URL'}, { 'description', 'Description'}, { 'price', 'Price ($)'} }
	
	for k, v in ipairs(desc) do
		v.label = guiCreateLabel(10, 22 * k + 3, 100, 22, v[2] .. ':', false, editing_window)
		guiLabelSetHorizontalAlign(v.label, 'right')
		

		v.edit = guiCreateEdit(115, 22 * k, editing_width - 125, 22, '', false, editing_window)
	end

 

Link to comment
47 minutes ago, LeonardoWilliams said:

How do i get the guiGetText from this ?

Each sub-table inside of the table stored inside the desc variable has four key-value pairs after script execution, for example: [1] = "skin", [2] = "Base Skin", ["label"] = gui-label, ["edit"] = gui-edit. You can obtain a single such gui-edit by...

desc[1].edit

... or you can loop through the desc table using an for-each ipairs loop and then do as eoL|Shady has suggested.

  • Like 1
Link to comment

Thank you so much The_GTA!!

What do i need to put in fetchRemote at URL, everything i put i get the errorcode 0, i put https://i.imgur.com/jmf91Em + .jpeg and it still gives me errorcode 1, if i delete the https:// i don`t get an error but the size is always 0.. Can you give me a perfect example?

function veziURL(thePlayer, command)
fetchRemote('i.imgur.com/jmf91Em.jpeg', callBackFetch)

end


function callBackFetch(responseData, errno)
if responseData == 'ERROR' then
outputChatBox('A aparut o eroare cu ID-ul '..errno, root, 255, 100, 100)
else
local myfile = fileCreate('learnfile')
fileWrite(myfile, responseData)
local filesize = fileGetSize(myfile)
outputChatBox('Marimea este de: '..filesize..' bytes!')
end
end


addCommandHandler('veziurlul', veziURL, false, false)

I made this script but the size is always 0 bytes :/

Link to comment
15 minutes ago, LeonardoWilliams said:

Thank you so much The_GTA!!

What do i need to put in fetchRemote at URL, everything i put i get the errorcode 0, i put https://i.imgur.com/jmf91Em + .jpeg and it still gives me errorcode 1, if i delete the https:// i don`t get an error but the size is always 0.. Can you give me a perfect example?

function veziURL(thePlayer, command)
fetchRemote('i.imgur.com/jmf91Em.jpeg', callBackFetch)

end


function callBackFetch(responseData, errno)
if responseData == 'ERROR' then
outputChatBox('A aparut o eroare cu ID-ul '..errno, root, 255, 100, 100)
else
local myfile = fileCreate('learnfile')
fileWrite(myfile, responseData)
local filesize = fileGetSize(myfile)
outputChatBox('Marimea este de: '..filesize..' bytes!')
end
end


addCommandHandler('veziurlul', veziURL, false, false)

I made this script but the size is always 0 bytes :/

If you get the error-code 1 then it is translated to the following internal error descriptor:

CURLE_UNSUPPORTED_PROTOCOL

It looks like MTA does not support connecting to the web-service that you have specified. I am judging this according to the official MTA documentation. This is a shame, really. I wonder if you could get competent help if you did post your inquiry on the MTA:SA GitHub issue tracker? Web-technology is really complicated and, as you see by this example, it is likely to find situations of incompatibility.

Hint: it is important to prefix the URL you pass into fetchRemote with either "http://" (not encrypted) or "https://" (encrypted). Else you are violating the spec.

Good luck!

Edited by The_GTA
Link to comment
20 minutes ago, LeonardoWilliams said:

Do you know any web`service that MTA supports for uploading photos such as textures, this is a system for skin textures that we upload on a specific site and i`m trying to make a MAX MB so players won`t be able to make skins that are higher than 1mb.

I wonder, since you plan to download the textures to your MTA server anyway, why do you not host your own image upload server on the same machine as your MTA server aswell? I know that it can be a hassle to upload files through MTA, especially if the users have to visit the MTA folder and put files there. But you can then place files that you download in a folder that MTA would look into, basically maintain the database in coordination with both the upload server and the MTA server.

This GitHub issue could be of interest to you: A way to upload files to a server · Issue #2489 · multitheftauto/mtasa-blue (github.com)

Edited by The_GTA
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...