BNA8/.resources/6f09fcfd0835066f388bfdd2f5cab0be2b1ff9b6f1c3bd9ce20ac34f6aa91784
2025-08-18 15:23:02 +04:00

104 lines
3.1 KiB
Plaintext

import os
import shutil
from com.inductiveautomation.ignition.common.model import ApplicationScope
os_symlink = getattr(os, "symlink", None)
if callable(os_symlink):
pass
else:
def symlink_ms(source, link_name):
import ctypes
csl = ctypes.windll.kernel32.CreateSymbolicLinkW
csl.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32)
csl.restype = ctypes.c_ubyte
flags = 1 if os.path.isdir(source) else 0
if csl(link_name, source, flags) == 0:
raise ctypes.WinError()
os.symlink = symlink_ms
#############
### Constants
#############
WP = os.path.join(os.getcwd(), "webserver", "webapps", "main", "documents")
DP = WP#"D:\\Ignition\\Documents"
#if not os.path.exists("D:\\"):
# Not on actual production server, so store locally:
#DP = "Ignition\\Documents"
if ApplicationScope.isGateway(ApplicationScope.getGlobalScope()) and not os.path.exists(DP):
os.makedirs(DP)
#os.symlink(DP, WP)
# The above symlink doesn't work on windows due to process permissions. Need to manually set up:
# mklink /J "C:\\Program Files\\Inductive Automation\\Ignition\\webserver\\webapps\\main\\documents" D:\\Ignition\\Documents
PDF_ICON = {
"path": "material/picture_as_pdf",
"color": "#869DB1",
"style": {}
}
#######################################################
#######################################################
#######################################################
#### Functions
#######################################################
def getFileTree(path, phantomPath=""):
files = []
for f in os.listdir(path):
filepath = os.path.join(path, f)
filephantompath = os.path.join(phantomPath, f)
# Check what type of file:
if os.path.isfile(filepath):
# Add leaf node (file) if pdf:
if f.endswith(".pdf"):
files.append({
"label": f,
"expanded": False,
"icon": PDF_ICON,
"data": filephantompath,
"items": []
})
elif os.path.isdir(filepath):
# Add folder:
files.append({
"label": f,
"expanded": False,
"data": "",
"items": getFileTree(filepath, filephantompath)
})
# Return list:
return files
def getDocuments():
# Return list:
return getFileTree(DP)
def createFolder(folderpath):
if not os.path.exists(folderpath):
os.makedirs(os.path.join(DP, folderpath))
def uploadDocument(file, folder=""):
filename = os.path.join(DP, folder, file.name)
if filename[-4:].lower() <> ".pdf":
system.perspective("File extension not compatible!\r\nThe supported extensions are: pdf")
else:
filepath = os.path.join(DP, filename)
#if system.file.fileExists(filepath):
#return "Config file already exists!\r\nYou must delete the config before uploading over it."
file.copyTo(filepath)
#system.file.writeFile(filepath, contents, False)
def deleteDocuments(filenames):
for filename in filenames:
filepath = os.path.join(DP, filename)
try:
if os.path.isfile(filepath):
os.remove(filepath)
elif os.path.isdir(filepath):
shutil.rmtree(filepath)
else:
pass#return "Config file doesn't exist!"
except:
system.perspective.print("Failed to delete "+filename)