Jump to content

Downloading files and using them across multiple sessions


.:HyPeX:.

Recommended Posts

Well, i want to make a download system in wich you would actually download images (With fetchRemote).

But, having said that, can i just directly draw them with dxDrawImage?

My idea was something like this:

  
function() 
if fetch.id then return end 
if not fileExists(":avatars/saved/id.png") then 
fetchRemote("page/uploads/photo-id.png") 
fetch.id = true 
end 
end 
  
-- onClientRender 
dxDrawImage(":avatars/saved/id.png") 
  
  

My issue is: Can i actually store the file and leave it there? (To avoid re-downloads mostly)

Else i would just make them re-download on each join but even thought most images are not big size, some can actually be 200kb, and said that it will be one for each player... i'd prefer to dont have to re-download them..

Link to comment
fileCreate 
fileWrite 
fileClose 

or you can simply use downloadFile

i need to set the images as attribute-false (And the IDs are managed by the website, so i cannot know them), so discard downloadFile..

How could i use fileWrite? i thought about this..

would this be correct?

  
local texture = text -- from fetchRemote 
local string = dxGetPixels(texture) 
local file = fileCreate("Id-photo.txt") 
fileWrite(file, string) 
fileClose(file) 
  
--- Later on 
  
-- onLoad:  
  
local file = fileOpen("Id-photo.txt") 
local string = fileRead(file, fileGetSize(file)) 
fileClose(file) 
local texture = dxCreateTexture(string, 128,128) 
  
  

Link to comment
Forget about downloadFile, I noticed as soon as I posted it, then rapidly edited post.

Anyway,

local string = dxGetPixels(texture) 

What's that? is not needed, just save the value fetchRemote returns.

As far as i know, the fetchRemote returns a material element and fileWrite needs a string :?:

https://wiki.multitheftauto.com/wiki/DxGetTexturePixels

I'm just converting the image to pixels.. (My bad, typo there)

Link to comment
Instead of Md5, you can also compare their exact sizes. It's faster than Md5 if you compare a ton of pretty big files. Not as safe though (can be tricked, but i'm sure there's no-one to make a useful file with exactly the same size). But I'd stick to Md5 if I were you, just like Dealman said.

No, nevermind about that, i'm using just forum avatars in-game. I just need a good way to check if users change them :D

Link to comment

Okay, i'm stuck in here... (ID is displayed as 0, but it shouldnt.)

SQL table:

X7CdVNm.png

  
  
addEventHandler('onPlayerLogin',root,function() 
local acc = getPlayerAccount(source) 
local acc = getAccountName(acc) 
local query = dbQuery(function(qh,player) 
local result = dbPoll(qh,0) 
local gvalue = 0 
for _,row in ipairs(result) do 
    for column,value in ipairs(row) do 
    if column == "member_id" then 
    gvalue = value 
    setElementData(source, "member_id", gvalue) 
    end 
    end 
end 
    outputConsole("id "..gvalue) 
        outputConsole("acc "..acc) 
local query = dbQuery(function(qh,player) 
local result = dbPoll(qh,0) 
id, photo = "","" 
for _,row in ipairs(result) do 
    for column,value in ipairs(row) do 
    if column == "pp_member_id" then 
    id = value 
    elseif column == "pp_main_photo" then 
    photo = value 
    setElementData(player, "avatar", value) 
    end 
    end 
    end 
     
    outputConsole("1 "..id.." "..photo) 
fetchImage(id, photo) 
  
end,{player},Database, "SELECT pp_member_id,pp_main_photo FROM profile_portal WHERE pp_member_id = '?'", gvalue) 
  
end,{source},Database, "SELECT member_id FROM members WHERE name='"..acc.."'") 
end) 
  
function fetchImage(id,photo) 
outputConsole("2 "..id.." "..photo) 
    fetchRemote ( "http://emp-clan.eu/uploads/"..photo, returnImage, "", false, id,photo ) 
end 
  
function returnImage(image, error, id, photo) 
outputConsole("3 "..id.." "..photo) 
    if fileExists("saved/"..photo) then 
    local file = fileOpen("saved/"..photo) 
    local size = fileGetSize(file) 
    fileClose(file) 
    local result = fileRead(file,size) 
    if result ~= image then 
    fileDelete("saved/"..photo) 
    local file = fileCreate("saved/"..photo) 
    fileWrite(file, image) 
    fileClose(file) 
    end 
    else 
    local file = fileCreate("saved/"..photo) 
    fileWrite(file, image) 
    fileClose(file) 
    end 
end 

Console output:

  
Press 'p' to open your admin panel 
login: You successfully logged in 
id 0 
acc hypex 
1   
2   
3   

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...