Add AdvDupe2.SanitizeFilename (#480)

* Add AdvDupe2.SanitizeFilename

* Uncomment

* Style
This commit is contained in:
Redox 2024-10-17 00:47:17 +02:00 committed by GitHub
parent 0ff4d3046c
commit 8a1fa8658f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 0 deletions

View File

@ -1,3 +1,13 @@
local invalidCharacters = { "\"", ":"}
function AdvDupe2.SanitizeFilename(filename)
for i=1, #invalidCharacters do
filename = string.gsub(filename, invalidCharacters[i], "_")
end
filename = string.gsub(filename, "%s+", " ")
return filename
end
local function AdvDupe2_ReceiveFile(len, ply) local function AdvDupe2_ReceiveFile(len, ply)
local AutoSave = net.ReadUInt(8) == 1 local AutoSave = net.ReadUInt(8) == 1
@ -18,6 +28,7 @@ local function AdvDupe2_ReceiveFile(len, ply)
path = AdvDupe2.GetFilename(AdvDupe2.SavePath) path = AdvDupe2.GetFilename(AdvDupe2.SavePath)
end end
path = AdvDupe2.SanitizeFilename(path)
local dupefile = file.Open(path, "wb", "DATA") local dupefile = file.Open(path, "wb", "DATA")
if(!dupefile)then if(!dupefile)then
AdvDupe2.Notify("File was not saved!",NOTIFY_ERROR,5) AdvDupe2.Notify("File was not saved!",NOTIFY_ERROR,5)

View File

@ -306,6 +306,8 @@ local function RenameFileCl(node, name)
AdvDupe2.Notify("Rename limit exceeded, could not rename.", NOTIFY_ERROR) AdvDupe2.Notify("Rename limit exceeded, could not rename.", NOTIFY_ERROR)
return return
end end
FilePath = AdvDupe2.SanitizeFilename(FilePath)
file.Write(FilePath, File) file.Write(FilePath, File)
if (file.Exists(FilePath, "DATA")) then if (file.Exists(FilePath, "DATA")) then
file.Delete(tempFilePath .. ".txt") file.Delete(tempFilePath .. ".txt")

View File

@ -524,6 +524,8 @@ if CLIENT then
local readFileName = "advdupe2/"..arg[1] local readFileName = "advdupe2/"..arg[1]
local writeFileName = "advdupe2/"..string.StripExtension(arg[1])..".json" local writeFileName = "advdupe2/"..string.StripExtension(arg[1])..".json"
writeFileName = AdvDupe2.SanitizeFilename(writeFileName)
local readFile = file.Open(readFileName, "rb", "DATA") local readFile = file.Open(readFileName, "rb", "DATA")
if not readFile then print("File could not be read or found! ("..readFileName..")") return end if not readFile then print("File could not be read or found! ("..readFileName..")") return end
local readData = readFile:Read(readFile:Size()) local readData = readFile:Read(readFile:Size())