mirror of
https://github.com/sky-systems/sky_phone.git
synced 2026-08-29 02:01:40 +00:00
ADD - added Extern Image/Video Import
This commit is contained in:
@@ -1557,10 +1557,26 @@ Locales["en"] = {
|
||||
deleteBody = "This photo or video will be permanently deleted.", deleted = "Media deleted.",
|
||||
zoomIn = "Zoom in", zoomOut = "Zoom out", resetZoom = "Reset zoom",
|
||||
filters = { all = "All", photos = "Photos", videos = "Videos" },
|
||||
import = {
|
||||
action = "Import", title = "Import", chooseSource = "Choose a website",
|
||||
linkTitle = "Import from Link", linkBody = "Paste a direct link to an image or video from this website.",
|
||||
linkLabel = "Image or video link", linkPlaceholder = "https://...", linkCompleted = "Media imported.",
|
||||
loading = "Loading media...", emptyTitle = "No Media",
|
||||
emptyBody = "This website has no media of this type.", loadMore = "Load More",
|
||||
alreadyImported = "Imported", failed = "Failed", completed = "Imported {imported} media.",
|
||||
partial = "Imported {imported} of {total} media.",
|
||||
},
|
||||
errors = {
|
||||
cancelled = "The media action was cancelled.", capture_failed = "Unable to capture the game view.",
|
||||
invalid_import_request = "The import request is invalid.", invalid_import_media = "This media item cannot be imported.",
|
||||
invalid_media_type = "The media type is invalid.", invalid_upload = "The upload could not be verified.",
|
||||
invalid_upload_token = "The upload session is no longer valid.", missing_config = "Gallery uploads are not configured.",
|
||||
import_media_not_allowed = "This media item is not allowed.", import_media_too_large = "This media item is too large.",
|
||||
import_media_unavailable = "This media item is no longer available.", import_provider_failed = "The website could not load its media.",
|
||||
import_provider_unauthorized = "The website credentials are invalid.", import_source_not_found = "This website is not registered.",
|
||||
import_source_unavailable = "This website is temporarily unavailable.",
|
||||
invalid_import_url = "Enter a valid HTTPS media link.", import_url_not_allowed = "This link is not from the selected website.",
|
||||
import_url_unavailable = "The linked media could not be reached.", import_size_unavailable = "The website did not provide the media size.",
|
||||
not_found = "The media item no longer exists.", owner_changed = "The active phone account changed.",
|
||||
operation_in_progress = "Another media operation is already in progress.",
|
||||
rate_limited = "Too many media actions. Try again shortly.", request_failed = "The Gallery request failed.",
|
||||
|
||||
@@ -10,6 +10,48 @@ Config.Media = {
|
||||
RequestTimeoutMs = 10000,
|
||||
UploadTimeoutMs = 25000,
|
||||
},
|
||||
Import = {
|
||||
Enabled = true,
|
||||
PageSize = 30,
|
||||
MaxSelection = 10,
|
||||
MaxPhotoBytes = 15 * 1024 * 1024,
|
||||
MaxVideoBytes = 150 * 1024 * 1024,
|
||||
RevalidateAfterSeconds = 3600,
|
||||
ListActionsPerMinute = 60,
|
||||
ImportActionsPerMinute = 20,
|
||||
CandidateTtlSeconds = 300,
|
||||
ManifestCacheSeconds = 30,
|
||||
ManifestMaxBytes = 2 * 1024 * 1024,
|
||||
ManifestMaxItems = 5000,
|
||||
Websites = {
|
||||
{
|
||||
Id = "fivemanage",
|
||||
Label = "FiveManage",
|
||||
Enabled = true,
|
||||
Adapter = "fivemanage",
|
||||
Path = "sky_phone/imports",
|
||||
MediaTypes = { "photo", "video" },
|
||||
-- Direct links entered in Gallery must use one of these hosts or a subdomain.
|
||||
AllowedMediaHosts = { "fivemanage.com" },
|
||||
},
|
||||
--[[
|
||||
{
|
||||
Id = "city_media",
|
||||
Label = "City Media",
|
||||
Enabled = true,
|
||||
Adapter = "manifest",
|
||||
ManifestUrl = "https://media.example.com/sky-phone/media.json",
|
||||
MediaTypes = { "photo", "video" },
|
||||
AllowedMediaHosts = { "media.example.com", "cdn.example.com" },
|
||||
Auth = {
|
||||
Type = "bearer",
|
||||
TokenConvar = "sky_phone_city_media_token",
|
||||
},
|
||||
RequiredAce = "sky_phone.import.city_media",
|
||||
},
|
||||
]]
|
||||
},
|
||||
},
|
||||
Photo = {
|
||||
Encoding = "jpg",
|
||||
Quality = 0.95,
|
||||
|
||||
@@ -71,6 +71,9 @@ server_scripts {
|
||||
'source/server/sim.lua',
|
||||
'source/server/payphones.lua',
|
||||
'source/server/calls.lua',
|
||||
'source/server/media_import.lua',
|
||||
'source/server/media_import/fivemanage.lua',
|
||||
'source/server/media_import/manifest.lua',
|
||||
'source/server/media.lua',
|
||||
'source/server/weazel_news.lua',
|
||||
'source/server/messages.lua',
|
||||
|
||||
@@ -265,6 +265,10 @@ local server_callbacks = {
|
||||
"flare:send",
|
||||
"gallery:list",
|
||||
"media:config",
|
||||
"media:import:sources",
|
||||
"media:import:list",
|
||||
"media:import:commit",
|
||||
"media:import:url",
|
||||
}
|
||||
|
||||
local function get_locale()
|
||||
@@ -637,6 +641,15 @@ for _, callback_name in ipairs(server_callbacks) do
|
||||
return
|
||||
end
|
||||
local result = Bridge.Callbacks.Trigger("sky_phone:" .. callback_name, data)
|
||||
if callback_name:match("^media:import:") and (not result or not result.success) then
|
||||
Bridge.Debug(
|
||||
"warn",
|
||||
"[sky_phone] NUI callback '%s' failed: %s.",
|
||||
callback_name,
|
||||
tostring(result and result.error or "no server response"),
|
||||
{ always = true }
|
||||
)
|
||||
end
|
||||
if type(result) == "table" then
|
||||
cb(result)
|
||||
return
|
||||
|
||||
@@ -378,6 +378,14 @@ local schema = {
|
||||
{ name = "remote_id", type = "VARCHAR(128) NOT NULL" },
|
||||
{ name = "media_type", type = "ENUM('photo', 'video') NOT NULL" },
|
||||
{ name = "mime_type", type = "VARCHAR(120) NULL" },
|
||||
{ name = "origin", type = "ENUM('phone_upload', 'website_import') NOT NULL DEFAULT 'phone_upload'" },
|
||||
{
|
||||
name = "source_id",
|
||||
type = "VARCHAR(64) NULL",
|
||||
characterSet = "ascii",
|
||||
collation = "ascii_bin",
|
||||
},
|
||||
{ name = "verified_at", type = "DATETIME NULL" },
|
||||
{ name = "created_at", type = "DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP" },
|
||||
},
|
||||
primaryKey = "id",
|
||||
@@ -2603,4 +2611,16 @@ Bridge.Database.EnsureIndex("sky_phone_devices", "uniq_sky_phone_devices_sim", "
|
||||
Bridge.Database.Query("UPDATE `sky_phone_contacts` SET `contact_id` = `id` WHERE `contact_id` IS NULL", {})
|
||||
Bridge.Database.EnsureIndex("sky_phone_contacts", "uniq_sky_phone_contacts_account_contact", "(`account_id`, `contact_id`)", { unique = true })
|
||||
Bridge.Database.EnsureIndex("sky_phone_contacts", "uniq_sky_phone_contacts_device_contact", "(`device_imei`, `contact_id`)", { unique = true })
|
||||
Bridge.Database.EnsureIndex(
|
||||
"sky_phone_media",
|
||||
"uniq_sky_phone_media_account_source",
|
||||
"(`account_id`, `source_id`, `remote_id`, `origin`)",
|
||||
{ unique = true }
|
||||
)
|
||||
Bridge.Database.EnsureIndex(
|
||||
"sky_phone_media",
|
||||
"uniq_sky_phone_media_device_source",
|
||||
"(`device_imei`, `source_id`, `remote_id`, `origin`)",
|
||||
{ unique = true }
|
||||
)
|
||||
Bridge.Database.CompleteMigration("sky_phone")
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
Bridge.Database.AfterMigration("sky_phone", function()
|
||||
SkyPhoneMedia = {}
|
||||
SkyPhoneMediaImport.Initialize()
|
||||
|
||||
local pending_uploads = {}
|
||||
local pending_deletes = {}
|
||||
@@ -76,7 +77,7 @@ local function request_presigned_url()
|
||||
if not api_configured() then
|
||||
return nil, "missing_config"
|
||||
end
|
||||
local config = media_config()
|
||||
local config = Config.Media.FiveManage
|
||||
local response = http_request(
|
||||
tostring(config.BaseUrl):gsub("/+$", "") .. "/presigned-url",
|
||||
"GET",
|
||||
@@ -99,9 +100,12 @@ local function get_remote_file(remote_id)
|
||||
if not api_configured() then
|
||||
return nil, "missing_config"
|
||||
end
|
||||
local config = media_config()
|
||||
local config = Config.Media.FiveManage
|
||||
local response = http_request(
|
||||
("%s/%s"):format(tostring(config.BaseUrl):gsub("/+$", ""), remote_id),
|
||||
("%s/%s"):format(
|
||||
tostring(config.BaseUrl):gsub("/+$", ""),
|
||||
SkyPhoneMediaImport.UrlEncode(remote_id)
|
||||
),
|
||||
"GET",
|
||||
"",
|
||||
{ ["Authorization"] = media_api_key() },
|
||||
@@ -114,9 +118,12 @@ local function delete_remote_file(remote_id)
|
||||
if not api_configured() then
|
||||
return false, "missing_config"
|
||||
end
|
||||
local config = media_config()
|
||||
local config = Config.Media.FiveManage
|
||||
local response = http_request(
|
||||
("%s/%s"):format(tostring(config.BaseUrl):gsub("/+$", ""), remote_id),
|
||||
("%s/%s"):format(
|
||||
tostring(config.BaseUrl):gsub("/+$", ""),
|
||||
SkyPhoneMediaImport.UrlEncode(remote_id)
|
||||
),
|
||||
"DELETE",
|
||||
"",
|
||||
{ ["Authorization"] = media_api_key() },
|
||||
@@ -171,7 +178,9 @@ function SkyPhoneMedia.ResolveOwnedMedia(source, media_id, media_type)
|
||||
params[#params + 1] = value
|
||||
end
|
||||
local rows = Bridge.Database.Query(([[
|
||||
SELECT `url`, `media_type`, `mime_type` FROM `sky_phone_media`
|
||||
SELECT `url`, `media_type`, `mime_type`, `origin`, `source_id`, `remote_id`,
|
||||
UNIX_TIMESTAMP(`verified_at`) AS `verified_at`
|
||||
FROM `sky_phone_media`
|
||||
WHERE `id` = ? AND %s
|
||||
LIMIT 1
|
||||
]]):format(condition), params)
|
||||
@@ -190,6 +199,34 @@ function SkyPhoneMedia.ResolveOwnedMedia(source, media_id, media_type)
|
||||
)
|
||||
return nil, "invalid_attachment"
|
||||
end
|
||||
if media.origin == "website_import" then
|
||||
local verified_at = tonumber(media.verified_at) or 0
|
||||
local revalidate_after = math.max(
|
||||
1,
|
||||
math.floor(tonumber(Config.Media.Import.RevalidateAfterSeconds) or 3600)
|
||||
)
|
||||
if os.time() - verified_at >= revalidate_after then
|
||||
local refreshed, refresh_error
|
||||
if type(media.remote_id) == "string" and media.remote_id:sub(1, 4) == "url:" then
|
||||
refreshed, refresh_error = SkyPhoneMediaImport.ResolveUrl(media.source_id, media.url)
|
||||
else
|
||||
refreshed, refresh_error = SkyPhoneMediaImport.Resolve(media.source_id, media.remote_id)
|
||||
end
|
||||
if not refreshed then
|
||||
return nil, refresh_error
|
||||
end
|
||||
if refreshed.mediaType ~= media_type then
|
||||
return nil, "import_media_not_allowed"
|
||||
end
|
||||
Bridge.Database.Query([[
|
||||
UPDATE `sky_phone_media`
|
||||
SET `url` = ?, `mime_type` = ?, `verified_at` = CURRENT_TIMESTAMP
|
||||
WHERE `id` = ? AND `origin` = 'website_import'
|
||||
]], { refreshed.url, refreshed.mimeType, id })
|
||||
media.url = refreshed.url
|
||||
media.mime_type = refreshed.mimeType
|
||||
end
|
||||
end
|
||||
return media.url, nil, media.mime_type
|
||||
end
|
||||
|
||||
@@ -499,7 +536,7 @@ RegisterNetEvent("sky_phone:media:request-upload", function(data)
|
||||
photo = Config.Media.Photo,
|
||||
presignedUrl = presigned_url,
|
||||
requestId = request_id,
|
||||
uploadTimeoutMs = media_config().UploadTimeoutMs,
|
||||
uploadTimeoutMs = Config.Media.FiveManage.UploadTimeoutMs,
|
||||
video = Config.Media.Video,
|
||||
})
|
||||
end)
|
||||
@@ -604,7 +641,7 @@ RegisterNetEvent("sky_phone:media:delete", function(data)
|
||||
query_params[#query_params + 1] = value
|
||||
end
|
||||
local rows = Bridge.Database.Query(([[
|
||||
SELECT `id`, `remote_id` FROM `sky_phone_media`
|
||||
SELECT `id`, `remote_id`, `origin` FROM `sky_phone_media`
|
||||
WHERE `id` = ? AND %s LIMIT 1
|
||||
]]):format(condition), query_params)
|
||||
local row = rows[1]
|
||||
@@ -612,32 +649,35 @@ RegisterNetEvent("sky_phone:media:delete", function(data)
|
||||
delete_result(src, correlation_id, false, "not_found", media_id)
|
||||
return
|
||||
end
|
||||
if pending_deletes[row.remote_id] then
|
||||
local delete_key = row.origin == "phone_upload" and row.remote_id or ("import:%s"):format(media_id)
|
||||
if pending_deletes[delete_key] then
|
||||
delete_result(src, correlation_id, false, "operation_in_progress", media_id)
|
||||
return
|
||||
end
|
||||
pending_deletes[row.remote_id] = src
|
||||
local references = Bridge.Database.Query(
|
||||
"SELECT COUNT(*) AS `count` FROM `sky_phone_media` WHERE `remote_id` = ?",
|
||||
{ row.remote_id }
|
||||
)
|
||||
if (tonumber(references[1] and references[1].count) or 0) <= 1 then
|
||||
local deleted, delete_error = delete_remote_file(row.remote_id)
|
||||
if not deleted then
|
||||
pending_deletes[row.remote_id] = nil
|
||||
delete_result(src, correlation_id, false, delete_error, media_id)
|
||||
return
|
||||
pending_deletes[delete_key] = src
|
||||
if row.origin == "phone_upload" then
|
||||
local references = Bridge.Database.Query(
|
||||
"SELECT COUNT(*) AS `count` FROM `sky_phone_media` WHERE `remote_id` = ?",
|
||||
{ row.remote_id }
|
||||
)
|
||||
if (tonumber(references[1] and references[1].count) or 0) <= 1 then
|
||||
local deleted, delete_error = delete_remote_file(row.remote_id)
|
||||
if not deleted then
|
||||
pending_deletes[delete_key] = nil
|
||||
delete_result(src, correlation_id, false, delete_error, media_id)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
Bridge.Database.Query(("DELETE FROM `sky_phone_media` WHERE `id` = ? AND %s"):format(condition), query_params)
|
||||
pending_deletes[row.remote_id] = nil
|
||||
pending_deletes[delete_key] = nil
|
||||
delete_result(src, correlation_id, true, nil, media_id)
|
||||
end)
|
||||
|
||||
function SkyPhoneMedia.GetDeviceRemoteIds(imei)
|
||||
local rows = Bridge.Database.Query([[
|
||||
SELECT `id`, `remote_id` FROM `sky_phone_media`
|
||||
WHERE `account_id` IS NULL AND `device_imei` = ?
|
||||
WHERE `account_id` IS NULL AND `device_imei` = ? AND `origin` = 'phone_upload'
|
||||
]], { imei })
|
||||
return rows
|
||||
end
|
||||
@@ -678,7 +718,7 @@ AddEventHandler("playerDropped", function()
|
||||
end)
|
||||
|
||||
if not api_configured() then
|
||||
print(("^3[sky_phone] Camera and Gallery uploads are disabled until the %s server convar is set.^7")
|
||||
print(("^3[sky_phone] Camera uploads and FiveManage imports are disabled until the %s server convar is set.^7")
|
||||
:format(tostring(media_config().ApiKeyConvar)))
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -0,0 +1,746 @@
|
||||
SkyPhoneMediaImport = {}
|
||||
|
||||
local adapters = {}
|
||||
local websites = {}
|
||||
local import_candidates = {}
|
||||
local initialized = false
|
||||
local media_types_by_mime = {
|
||||
["image/gif"] = "photo",
|
||||
["image/jpeg"] = "photo",
|
||||
["image/png"] = "photo",
|
||||
["image/webp"] = "photo",
|
||||
["video/mp4"] = "video",
|
||||
["video/quicktime"] = "video",
|
||||
["video/webm"] = "video",
|
||||
}
|
||||
|
||||
local function session_owner(source)
|
||||
local session, error_response = SkyPhone.RequireSession(source)
|
||||
if not session then
|
||||
return nil, error_response
|
||||
end
|
||||
|
||||
local device = SkyPhone.LoadDevice(session.imei)
|
||||
if not device then
|
||||
return nil, { success = false, error = "device_not_found" }
|
||||
end
|
||||
|
||||
return {
|
||||
account_id = device.account_id and tonumber(device.account_id) or nil,
|
||||
imei = session.imei,
|
||||
}
|
||||
end
|
||||
|
||||
local function owner_condition(owner)
|
||||
if owner.account_id then
|
||||
return "`account_id` = ?", { owner.account_id }
|
||||
end
|
||||
|
||||
return "`account_id` IS NULL AND `device_imei` = ?", { owner.imei }
|
||||
end
|
||||
|
||||
local function valid_source_id(value)
|
||||
return type(value) == "string"
|
||||
and #value >= 1
|
||||
and #value <= 64
|
||||
and value:match("^[a-z0-9_%-]+$") ~= nil
|
||||
end
|
||||
|
||||
local function valid_external_id(value)
|
||||
return type(value) == "string"
|
||||
and #value >= 1
|
||||
and #value <= 128
|
||||
and value:match("^[%w_.:%-]+$") ~= nil
|
||||
end
|
||||
|
||||
local function website_accessible(source, website)
|
||||
return not website.RequiredAce or website.RequiredAce == "" or IsPlayerAceAllowed(source, website.RequiredAce)
|
||||
end
|
||||
|
||||
local function media_type_set(values)
|
||||
local allowed = {}
|
||||
if type(values) ~= "table" then
|
||||
return allowed
|
||||
end
|
||||
|
||||
for _, value in ipairs(values) do
|
||||
if value == "photo" or value == "video" then
|
||||
allowed[value] = true
|
||||
end
|
||||
end
|
||||
return allowed
|
||||
end
|
||||
|
||||
local function url_host(value)
|
||||
if type(value) ~= "string" or #value > Config.Media.UrlMaxLength or value:find("%c") then
|
||||
return nil
|
||||
end
|
||||
|
||||
local authority = value:match("^https://([^/%?#]+)")
|
||||
if not authority or authority:find("@", 1, true) then
|
||||
return nil
|
||||
end
|
||||
|
||||
local host = authority:match("^([^:]+)")
|
||||
return host and host:lower() or nil
|
||||
end
|
||||
|
||||
function SkyPhoneMediaImport.ResponseHeader(headers, name)
|
||||
if type(headers) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
local requested_name = name:lower()
|
||||
for header_name, value in pairs(headers) do
|
||||
if type(header_name) == "string" and header_name:lower() == requested_name then
|
||||
if type(value) == "table" then
|
||||
return value[1]
|
||||
end
|
||||
return value
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function allowed_host(website, host)
|
||||
for _, configured_host in ipairs(website.AllowedMediaHosts) do
|
||||
local candidate = type(configured_host) == "string" and configured_host:lower():gsub("^%.", "") or ""
|
||||
if candidate ~= "" and (host == candidate or host:sub(-#candidate - 1) == "." .. candidate) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function normalize_media(website, item)
|
||||
if type(item) ~= "table" or not valid_external_id(item.externalId) then
|
||||
return nil, "invalid_import_media"
|
||||
end
|
||||
|
||||
local media_type = item.mediaType
|
||||
if not website._media_types[media_type] then
|
||||
return nil, "import_media_not_allowed"
|
||||
end
|
||||
|
||||
local mime_type = type(item.mimeType) == "string"
|
||||
and item.mimeType:lower():match("^%s*([^;%s]+)") or nil
|
||||
if not mime_type or media_types_by_mime[mime_type] ~= media_type then
|
||||
local path = type(item.url) == "string" and item.url:match("^https://[^/]+(/[^?#]*)") or nil
|
||||
local extension = path and path:match("%.([%w]+)$") or nil
|
||||
local mime_by_extension = {
|
||||
gif = "image/gif",
|
||||
jpeg = "image/jpeg",
|
||||
jpg = "image/jpeg",
|
||||
mov = "video/quicktime",
|
||||
mp4 = "video/mp4",
|
||||
png = "image/png",
|
||||
webm = "video/webm",
|
||||
webp = "image/webp",
|
||||
}
|
||||
mime_type = extension and mime_by_extension[extension:lower()] or nil
|
||||
end
|
||||
|
||||
local size = tonumber(item.size)
|
||||
if not size or size <= 0 or size ~= math.floor(size) then
|
||||
return nil, "invalid_import_media"
|
||||
end
|
||||
|
||||
local size_limit = media_type == "photo"
|
||||
and tonumber(Config.Media.Import.MaxPhotoBytes)
|
||||
or tonumber(Config.Media.Import.MaxVideoBytes)
|
||||
if not size_limit or size > size_limit then
|
||||
return nil, "import_media_too_large"
|
||||
end
|
||||
|
||||
local host = url_host(item.url)
|
||||
if not host or not allowed_host(website, host) then
|
||||
return nil, "import_media_not_allowed"
|
||||
end
|
||||
|
||||
local filename = type(item.filename) == "string" and item.filename:match("^%s*(.-)%s*$") or ""
|
||||
if filename == "" then
|
||||
filename = item.externalId
|
||||
elseif #filename > 160 then
|
||||
filename = filename:sub(1, 160)
|
||||
end
|
||||
|
||||
return {
|
||||
externalId = item.externalId,
|
||||
filename = filename,
|
||||
mediaType = media_type,
|
||||
mimeType = mime_type,
|
||||
size = size,
|
||||
sourceId = website.Id,
|
||||
url = item.url,
|
||||
}
|
||||
end
|
||||
|
||||
local function remember_candidate(source, media)
|
||||
local expires_at = os.time() + math.max(
|
||||
30,
|
||||
math.floor(tonumber(Config.Media.Import.CandidateTtlSeconds) or 300)
|
||||
)
|
||||
import_candidates[source] = import_candidates[source] or {}
|
||||
import_candidates[source][media.sourceId] = import_candidates[source][media.sourceId] or {}
|
||||
import_candidates[source][media.sourceId][media.externalId] = expires_at
|
||||
end
|
||||
|
||||
local function candidate_allowed(source, source_id, external_id)
|
||||
local by_source = import_candidates[source] and import_candidates[source][source_id]
|
||||
local expires_at = by_source and by_source[external_id]
|
||||
if not expires_at or expires_at < os.time() then
|
||||
if by_source then
|
||||
by_source[external_id] = nil
|
||||
end
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function validate_website(definition)
|
||||
if type(definition) ~= "table"
|
||||
or definition.Enabled == false
|
||||
or not valid_source_id(definition.Id)
|
||||
or type(definition.Label) ~= "string"
|
||||
or definition.Label == ""
|
||||
or #definition.Label > 64
|
||||
or type(definition.Adapter) ~= "string"
|
||||
then
|
||||
return nil, "invalid_definition"
|
||||
end
|
||||
|
||||
local adapter = adapters[definition.Adapter]
|
||||
if not adapter then
|
||||
return nil, "unknown_adapter"
|
||||
end
|
||||
|
||||
if type(definition.AllowedMediaHosts) ~= "table" or #definition.AllowedMediaHosts < 1 then
|
||||
return nil, "missing_allowed_hosts"
|
||||
end
|
||||
|
||||
local allowed_media_types = media_type_set(definition.MediaTypes)
|
||||
if not allowed_media_types.photo and not allowed_media_types.video then
|
||||
return nil, "missing_media_types"
|
||||
end
|
||||
|
||||
if definition.RequiredAce ~= nil and type(definition.RequiredAce) ~= "string" then
|
||||
return nil, "invalid_required_ace"
|
||||
end
|
||||
|
||||
definition._adapter = adapter
|
||||
definition._media_types = allowed_media_types
|
||||
local valid, validation_error = adapter.Validate(definition)
|
||||
if not valid then
|
||||
return nil, validation_error
|
||||
end
|
||||
|
||||
return definition
|
||||
end
|
||||
|
||||
local function build_registry()
|
||||
websites = {}
|
||||
local config = Config.Media.Import
|
||||
if not config.Enabled then
|
||||
return
|
||||
end
|
||||
|
||||
for index, definition in ipairs(config.Websites or {}) do
|
||||
local website, website_error = validate_website(definition)
|
||||
if website then
|
||||
if websites[website.Id] then
|
||||
error(("[sky_phone] Duplicate media import website id '%s'."):format(website.Id))
|
||||
end
|
||||
websites[website.Id] = website
|
||||
else
|
||||
Bridge.Debug(
|
||||
"warn",
|
||||
"[sky_phone] Media import website at index %s was disabled: %s.",
|
||||
tostring(index),
|
||||
tostring(website_error)
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function find_imported(owner, source_id, items)
|
||||
if #items == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local condition, params = owner_condition(owner)
|
||||
params[#params + 1] = source_id
|
||||
local placeholders = {}
|
||||
for index, item in ipairs(items) do
|
||||
placeholders[index] = "?"
|
||||
params[#params + 1] = item.externalId
|
||||
end
|
||||
|
||||
local rows = Bridge.Database.Query(([[
|
||||
SELECT `remote_id` FROM `sky_phone_media`
|
||||
WHERE %s AND `origin` = 'website_import' AND `source_id` = ?
|
||||
AND `remote_id` IN (%s)
|
||||
]]):format(condition, table.concat(placeholders, ", ")), params)
|
||||
local imported = {}
|
||||
for _, row in ipairs(rows) do
|
||||
imported[row.remote_id] = true
|
||||
end
|
||||
for _, item in ipairs(items) do
|
||||
item.imported = imported[item.externalId] or false
|
||||
end
|
||||
end
|
||||
|
||||
local function select_owned_import(owner, source_id, remote_id)
|
||||
local condition, params = owner_condition(owner)
|
||||
params[#params + 1] = source_id
|
||||
params[#params + 1] = remote_id
|
||||
local rows = Bridge.Database.Query(([[
|
||||
SELECT `id`, `url`, `media_type` AS `mediaType`, `mime_type` AS `mimeType`,
|
||||
UNIX_TIMESTAMP(`created_at`) * 1000 AS `createdAt`
|
||||
FROM `sky_phone_media`
|
||||
WHERE %s AND `origin` = 'website_import'
|
||||
AND `source_id` = ? AND `remote_id` = ?
|
||||
LIMIT 1
|
||||
]]):format(condition), params)
|
||||
local row = rows[1]
|
||||
if row then
|
||||
row.id = tonumber(row.id)
|
||||
row.createdAt = tonumber(row.createdAt) or 0
|
||||
end
|
||||
return row
|
||||
end
|
||||
|
||||
local function store_import(owner, media)
|
||||
local existing = select_owned_import(owner, media.sourceId, media.externalId)
|
||||
if existing then
|
||||
Bridge.Database.Query([[
|
||||
UPDATE `sky_phone_media`
|
||||
SET `url` = ?, `media_type` = ?, `mime_type` = ?, `verified_at` = CURRENT_TIMESTAMP
|
||||
WHERE `id` = ?
|
||||
]], { media.url, media.mediaType, media.mimeType, existing.id })
|
||||
existing.url = media.url
|
||||
existing.mediaType = media.mediaType
|
||||
existing.mimeType = media.mimeType
|
||||
return existing
|
||||
end
|
||||
|
||||
local result
|
||||
if owner.account_id then
|
||||
result = Bridge.Database.Query([[
|
||||
INSERT IGNORE INTO `sky_phone_media`
|
||||
(`account_id`, `device_imei`, `url`, `remote_id`, `media_type`, `mime_type`, `origin`, `source_id`, `verified_at`)
|
||||
VALUES (?, NULL, ?, ?, ?, ?, 'website_import', ?, CURRENT_TIMESTAMP)
|
||||
]], { owner.account_id, media.url, media.externalId, media.mediaType, media.mimeType, media.sourceId })
|
||||
else
|
||||
result = Bridge.Database.Query([[
|
||||
INSERT IGNORE INTO `sky_phone_media`
|
||||
(`account_id`, `device_imei`, `url`, `remote_id`, `media_type`, `mime_type`, `origin`, `source_id`, `verified_at`)
|
||||
VALUES (NULL, ?, ?, ?, ?, ?, 'website_import', ?, CURRENT_TIMESTAMP)
|
||||
]], { owner.imei, media.url, media.externalId, media.mediaType, media.mimeType, media.sourceId })
|
||||
end
|
||||
|
||||
local media_id = type(result) == "number" and result or (type(result) == "table" and tonumber(result.insertId))
|
||||
if media_id and media_id < 1 then
|
||||
media_id = nil
|
||||
end
|
||||
if not media_id then
|
||||
local concurrent = select_owned_import(owner, media.sourceId, media.externalId)
|
||||
if concurrent then
|
||||
return concurrent
|
||||
end
|
||||
return nil, "request_failed"
|
||||
end
|
||||
|
||||
return {
|
||||
createdAt = os.time() * 1000,
|
||||
id = media_id,
|
||||
mediaType = media.mediaType,
|
||||
url = media.url,
|
||||
}
|
||||
end
|
||||
|
||||
function SkyPhoneMediaImport.RegisterAdapter(name, adapter)
|
||||
assert(type(name) == "string" and name ~= "", "Media import adapter name must be a string")
|
||||
assert(type(adapter) == "table", "Media import adapter must be a table")
|
||||
assert(type(adapter.Validate) == "function", "Media import adapter requires Validate")
|
||||
assert(type(adapter.List) == "function", "Media import adapter requires List")
|
||||
assert(type(adapter.Resolve) == "function", "Media import adapter requires Resolve")
|
||||
assert(not adapters[name], ("Media import adapter '%s' is already registered"):format(name))
|
||||
adapters[name] = adapter
|
||||
end
|
||||
|
||||
function SkyPhoneMediaImport.HttpRequest(url, headers, timeout_ms, method)
|
||||
local request = promise.new()
|
||||
local settled = false
|
||||
local request_method = method or "GET"
|
||||
local request_host = url_host(url) or "invalid-host"
|
||||
PerformHttpRequest(url, function(status, response_body, response_headers, error_data)
|
||||
if settled then
|
||||
return
|
||||
end
|
||||
settled = true
|
||||
local response_status = tonumber(status) or 0
|
||||
if response_status == 0 then
|
||||
Bridge.Debug(
|
||||
"warn",
|
||||
"[sky_phone] Media import HTTP %s request to '%s' failed: %s.",
|
||||
request_method,
|
||||
request_host,
|
||||
tostring(error_data or "unknown transport error"),
|
||||
{ always = true }
|
||||
)
|
||||
elseif response_status >= 400 then
|
||||
Bridge.Debug(
|
||||
"debug",
|
||||
"[sky_phone] Media import HTTP %s request to '%s' returned status %s.",
|
||||
request_method,
|
||||
request_host,
|
||||
tostring(response_status)
|
||||
)
|
||||
end
|
||||
request:resolve({
|
||||
body = response_body or "",
|
||||
error = error_data,
|
||||
headers = response_headers or {},
|
||||
status = response_status,
|
||||
})
|
||||
end, request_method, "", headers or {}, { followLocation = false })
|
||||
SetTimeout(timeout_ms, function()
|
||||
if settled then
|
||||
return
|
||||
end
|
||||
settled = true
|
||||
Bridge.Debug(
|
||||
"warn",
|
||||
"[sky_phone] Media import HTTP %s request to '%s' timed out after %s ms.",
|
||||
request_method,
|
||||
request_host,
|
||||
tostring(timeout_ms),
|
||||
{ always = true }
|
||||
)
|
||||
request:resolve({ body = "", error = "request timeout", headers = {}, status = 0 })
|
||||
end)
|
||||
return Citizen.Await(request)
|
||||
end
|
||||
|
||||
function SkyPhoneMediaImport.ResolveUrl(source_id, url)
|
||||
if not initialized or not valid_source_id(source_id) or type(url) ~= "string" then
|
||||
return nil, "invalid_import_url"
|
||||
end
|
||||
|
||||
local website = websites[source_id]
|
||||
local trimmed_url = url:match("^%s*(.-)%s*$")
|
||||
local host = website and url_host(trimmed_url) or nil
|
||||
if not website or not host or not allowed_host(website, host) then
|
||||
return nil, "import_url_not_allowed"
|
||||
end
|
||||
|
||||
if type(website._adapter.ResolveUrl) == "function" then
|
||||
local item, resolve_error = website._adapter.ResolveUrl(website, trimmed_url)
|
||||
if not item then
|
||||
return nil, resolve_error
|
||||
end
|
||||
return normalize_media(website, item)
|
||||
end
|
||||
|
||||
local response = SkyPhoneMediaImport.HttpRequest(
|
||||
trimmed_url,
|
||||
{},
|
||||
tonumber(website.RequestTimeoutMs or Config.Media.FiveManage.RequestTimeoutMs) or 10000,
|
||||
"HEAD"
|
||||
)
|
||||
if response.status == 0 then
|
||||
return nil, "import_source_unavailable"
|
||||
end
|
||||
if response.status < 200 or response.status >= 300 then
|
||||
return nil, "import_url_unavailable"
|
||||
end
|
||||
|
||||
local content_type = SkyPhoneMediaImport.ResponseHeader(response.headers, "content-type")
|
||||
content_type = type(content_type) == "string" and content_type:lower():match("^%s*([^;%s]+)") or nil
|
||||
local media_type = content_type and media_types_by_mime[content_type] or nil
|
||||
if not media_type or not website._media_types[media_type] then
|
||||
return nil, "import_media_not_allowed"
|
||||
end
|
||||
|
||||
local content_length = tonumber(SkyPhoneMediaImport.ResponseHeader(response.headers, "content-length"))
|
||||
if not content_length or content_length <= 0 or content_length ~= math.floor(content_length) then
|
||||
return nil, "import_size_unavailable"
|
||||
end
|
||||
|
||||
local external_id = ("url:%08x%08x"):format(
|
||||
joaat(trimmed_url) & 0xffffffff,
|
||||
joaat("sky_phone:" .. trimmed_url) & 0xffffffff
|
||||
)
|
||||
local url_path = trimmed_url:match("^https://[^/]+(/[^?#]*)") or ""
|
||||
return normalize_media(website, {
|
||||
externalId = external_id,
|
||||
filename = url_path:match("/([^/]+)$") or external_id,
|
||||
mediaType = media_type,
|
||||
mimeType = content_type,
|
||||
size = content_length,
|
||||
url = trimmed_url,
|
||||
})
|
||||
end
|
||||
|
||||
function SkyPhoneMediaImport.UrlEncode(value)
|
||||
return tostring(value):gsub("\n", "\r\n"):gsub("([^%w%-_%.~])", function(character)
|
||||
return ("%%%02X"):format(character:byte())
|
||||
end)
|
||||
end
|
||||
|
||||
function SkyPhoneMediaImport.Resolve(source_id, external_id)
|
||||
if not initialized or not valid_source_id(source_id) or not valid_external_id(external_id) then
|
||||
return nil, "import_source_unavailable"
|
||||
end
|
||||
|
||||
local website = websites[source_id]
|
||||
if not website then
|
||||
return nil, "import_source_unavailable"
|
||||
end
|
||||
|
||||
local item, resolve_error = website._adapter.Resolve(website, external_id)
|
||||
if not item then
|
||||
return nil, resolve_error
|
||||
end
|
||||
|
||||
return normalize_media(website, item)
|
||||
end
|
||||
|
||||
function SkyPhoneMediaImport.Initialize()
|
||||
assert(not initialized, "Media import was initialized more than once")
|
||||
build_registry()
|
||||
initialized = true
|
||||
|
||||
Bridge.Callbacks.Register("sky_phone:media:import:sources", function(source)
|
||||
local owner, error_response = session_owner(source)
|
||||
if not owner then
|
||||
return error_response
|
||||
end
|
||||
|
||||
local sources = {}
|
||||
for _, website in pairs(websites) do
|
||||
if website_accessible(source, website) then
|
||||
local media_types = {}
|
||||
if website._media_types.photo then
|
||||
media_types[#media_types + 1] = "photo"
|
||||
end
|
||||
if website._media_types.video then
|
||||
media_types[#media_types + 1] = "video"
|
||||
end
|
||||
sources[#sources + 1] = {
|
||||
id = website.Id,
|
||||
label = website.Label,
|
||||
mediaTypes = media_types,
|
||||
}
|
||||
end
|
||||
end
|
||||
table.sort(sources, function(left, right)
|
||||
return left.label:lower() < right.label:lower()
|
||||
end)
|
||||
return {
|
||||
success = true,
|
||||
data = {
|
||||
maxSelection = math.max(1, math.floor(tonumber(Config.Media.Import.MaxSelection) or 1)),
|
||||
sources = sources,
|
||||
},
|
||||
}
|
||||
end)
|
||||
|
||||
Bridge.Callbacks.Register("sky_phone:media:import:list", function(source, data)
|
||||
local owner, error_response = session_owner(source)
|
||||
if not owner then
|
||||
return error_response
|
||||
end
|
||||
if not SkyPhone.AllowOperation(
|
||||
source,
|
||||
"media_import_list",
|
||||
tonumber(Config.Media.Import.ListActionsPerMinute) or 60,
|
||||
60
|
||||
) then
|
||||
return { success = false, error = "rate_limited" }
|
||||
end
|
||||
|
||||
data = type(data) == "table" and data or {}
|
||||
local website = valid_source_id(data.sourceId) and websites[data.sourceId] or nil
|
||||
local media_type = data.mediaType
|
||||
local page = math.floor(tonumber(data.page) or 1)
|
||||
if not website or not website_accessible(source, website) then
|
||||
return { success = false, error = "import_source_not_found" }
|
||||
end
|
||||
if not website._media_types[media_type] or page < 1 or page > 10000 then
|
||||
return { success = false, error = "invalid_import_request" }
|
||||
end
|
||||
|
||||
local limit = math.max(1, math.min(math.floor(tonumber(Config.Media.Import.PageSize) or 30), 100))
|
||||
local result, list_error = website._adapter.List(website, media_type, page, limit)
|
||||
if not result then
|
||||
return { success = false, error = list_error }
|
||||
end
|
||||
if type(result) ~= "table" or type(result.items) ~= "table" then
|
||||
Bridge.Debug(
|
||||
"warn",
|
||||
"[sky_phone] Import source '%s' returned an invalid list response.",
|
||||
website.Id
|
||||
)
|
||||
return { success = false, error = "import_provider_failed" }
|
||||
end
|
||||
|
||||
local items = {}
|
||||
for _, item in ipairs(result.items) do
|
||||
local normalized, normalize_error = normalize_media(website, item)
|
||||
if normalized then
|
||||
items[#items + 1] = normalized
|
||||
remember_candidate(source, normalized)
|
||||
else
|
||||
Bridge.Debug(
|
||||
"warn",
|
||||
"[sky_phone] Rejected media '%s' from import source '%s': %s.",
|
||||
tostring(item.externalId),
|
||||
website.Id,
|
||||
tostring(normalize_error)
|
||||
)
|
||||
end
|
||||
end
|
||||
find_imported(owner, website.Id, items)
|
||||
return {
|
||||
success = true,
|
||||
data = {
|
||||
hasMore = result.hasMore == true,
|
||||
items = items,
|
||||
page = page,
|
||||
total = math.max(0, math.floor(tonumber(result.total) or #items)),
|
||||
},
|
||||
}
|
||||
end)
|
||||
|
||||
Bridge.Callbacks.Register("sky_phone:media:import:commit", function(source, data)
|
||||
local owner, error_response = session_owner(source)
|
||||
if not owner then
|
||||
return error_response
|
||||
end
|
||||
if not SkyPhone.AllowOperation(
|
||||
source,
|
||||
"media_import_commit",
|
||||
tonumber(Config.Media.Import.ImportActionsPerMinute) or 20,
|
||||
60
|
||||
) then
|
||||
return { success = false, error = "rate_limited" }
|
||||
end
|
||||
|
||||
data = type(data) == "table" and data or {}
|
||||
local website = valid_source_id(data.sourceId) and websites[data.sourceId] or nil
|
||||
if not website or not website_accessible(source, website) then
|
||||
return { success = false, error = "import_source_not_found" }
|
||||
end
|
||||
if type(data.externalIds) ~= "table" then
|
||||
return { success = false, error = "invalid_import_request" }
|
||||
end
|
||||
|
||||
local maximum = math.max(1, math.floor(tonumber(Config.Media.Import.MaxSelection) or 1))
|
||||
if #data.externalIds < 1 or #data.externalIds > maximum then
|
||||
return { success = false, error = "invalid_import_request" }
|
||||
end
|
||||
|
||||
local unique_ids = {}
|
||||
local requested_ids = {}
|
||||
for _, external_id in ipairs(data.externalIds) do
|
||||
if not valid_external_id(external_id) or unique_ids[external_id] then
|
||||
return { success = false, error = "invalid_import_request" }
|
||||
end
|
||||
if not candidate_allowed(source, website.Id, external_id) then
|
||||
return { success = false, error = "invalid_import_request" }
|
||||
end
|
||||
unique_ids[external_id] = true
|
||||
requested_ids[#requested_ids + 1] = external_id
|
||||
end
|
||||
|
||||
local imported = {}
|
||||
local failed = {}
|
||||
for _, external_id in ipairs(requested_ids) do
|
||||
local item, resolve_error = website._adapter.Resolve(website, external_id)
|
||||
local normalized, normalize_error
|
||||
if item then
|
||||
normalized, normalize_error = normalize_media(website, item)
|
||||
end
|
||||
if not normalized then
|
||||
failed[#failed + 1] = {
|
||||
error = resolve_error or normalize_error or "import_provider_failed",
|
||||
externalId = external_id,
|
||||
}
|
||||
else
|
||||
local stored, store_error = store_import(owner, normalized)
|
||||
if stored then
|
||||
imported[#imported + 1] = stored
|
||||
else
|
||||
failed[#failed + 1] = {
|
||||
error = store_error or "request_failed",
|
||||
externalId = external_id,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
success = true,
|
||||
data = {
|
||||
failed = failed,
|
||||
imported = imported,
|
||||
},
|
||||
}
|
||||
end)
|
||||
|
||||
Bridge.Callbacks.Register("sky_phone:media:import:url", function(source, data)
|
||||
local owner, error_response = session_owner(source)
|
||||
if not owner then
|
||||
return error_response
|
||||
end
|
||||
if not SkyPhone.AllowOperation(
|
||||
source,
|
||||
"media_import_url",
|
||||
tonumber(Config.Media.Import.ImportActionsPerMinute) or 20,
|
||||
60
|
||||
) then
|
||||
return { success = false, error = "rate_limited" }
|
||||
end
|
||||
|
||||
data = type(data) == "table" and data or {}
|
||||
local website = valid_source_id(data.sourceId) and websites[data.sourceId] or nil
|
||||
if not website or not website_accessible(source, website) then
|
||||
return { success = false, error = "import_source_not_found" }
|
||||
end
|
||||
if type(data.url) ~= "string" or #data.url < 1 or #data.url > Config.Media.UrlMaxLength then
|
||||
return { success = false, error = "invalid_import_url" }
|
||||
end
|
||||
|
||||
local normalized, resolve_error = SkyPhoneMediaImport.ResolveUrl(website.Id, data.url)
|
||||
if not normalized then
|
||||
Bridge.Debug(
|
||||
"warn",
|
||||
"[sky_phone] Media URL import failed for player %s, source '%s', host '%s': %s.",
|
||||
tostring(source),
|
||||
website.Id,
|
||||
tostring(url_host(data.url) or "invalid-host"),
|
||||
tostring(resolve_error),
|
||||
{ always = true }
|
||||
)
|
||||
return { success = false, error = resolve_error }
|
||||
end
|
||||
Bridge.Debug(
|
||||
"debug",
|
||||
"[sky_phone] Media URL import resolved for player %s via source '%s' as %s '%s' (%s bytes).",
|
||||
tostring(source),
|
||||
website.Id,
|
||||
normalized.mediaType,
|
||||
normalized.externalId,
|
||||
tostring(normalized.size)
|
||||
)
|
||||
local stored, store_error = store_import(owner, normalized)
|
||||
if not stored then
|
||||
return { success = false, error = store_error or "request_failed" }
|
||||
end
|
||||
return { success = true, data = stored }
|
||||
end)
|
||||
|
||||
AddEventHandler("playerDropped", function()
|
||||
import_candidates[source] = nil
|
||||
end)
|
||||
end
|
||||
@@ -0,0 +1,240 @@
|
||||
local function api_key(website)
|
||||
local convar = website.ApiKeyConvar or Config.Media.FiveManage.ApiKeyConvar
|
||||
if type(convar) ~= "string" or convar == "" then
|
||||
return ""
|
||||
end
|
||||
return GetConvar(convar, "")
|
||||
end
|
||||
|
||||
local function provider_error(response, not_found_error)
|
||||
if response.status == 0 then
|
||||
return "import_source_unavailable"
|
||||
end
|
||||
if response.status == 401 or response.status == 403 then
|
||||
return "import_provider_unauthorized"
|
||||
end
|
||||
if response.status == 404 and not_found_error then
|
||||
return not_found_error
|
||||
end
|
||||
return "import_provider_failed"
|
||||
end
|
||||
|
||||
local function decode_response(response, not_found_error)
|
||||
if type(response) ~= "table" or response.status < 200 or response.status >= 300 then
|
||||
return nil, provider_error(response or { status = 0 }, not_found_error)
|
||||
end
|
||||
|
||||
local success, decoded = pcall(json.decode, response.body or "")
|
||||
if not success or type(decoded) ~= "table" then
|
||||
return nil, "import_provider_failed"
|
||||
end
|
||||
return decoded
|
||||
end
|
||||
|
||||
local function media_type(value)
|
||||
local normalized = type(value) == "string" and value:lower() or ""
|
||||
if normalized == "image" or normalized:find("image/", 1, true) == 1 then
|
||||
return "photo"
|
||||
end
|
||||
if normalized == "video" or normalized:find("video/", 1, true) == 1 then
|
||||
return "video"
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local media_extensions = {
|
||||
gif = true,
|
||||
jpeg = true,
|
||||
jpg = true,
|
||||
mov = true,
|
||||
mp4 = true,
|
||||
png = true,
|
||||
webm = true,
|
||||
webp = true,
|
||||
}
|
||||
|
||||
local function public_file_id(url)
|
||||
local path = type(url) == "string" and url:match("^https://[^/%?#]+(/[^?#]*)") or nil
|
||||
local segment = path and path:match("/([^/]+)$") or nil
|
||||
if not segment or segment == "" or segment:find("%", 1, true) then
|
||||
return nil
|
||||
end
|
||||
|
||||
local extension = segment:match("%.([%w]+)$")
|
||||
if extension and media_extensions[extension:lower()] then
|
||||
segment = segment:sub(1, -#extension - 2)
|
||||
end
|
||||
if #segment < 1 or #segment > 128 or not segment:match("^[%w_%-]+$") then
|
||||
return nil
|
||||
end
|
||||
return segment
|
||||
end
|
||||
|
||||
local function normalize_file(file)
|
||||
if type(file) ~= "table" then
|
||||
return {}
|
||||
end
|
||||
return {
|
||||
externalId = file.id,
|
||||
filename = file.filename,
|
||||
mediaType = media_type(file.type or file.mimeType),
|
||||
mimeType = file.mimeType or file.type,
|
||||
size = file.size,
|
||||
url = file.url,
|
||||
}
|
||||
end
|
||||
|
||||
local function resolve_file(website, external_id)
|
||||
local provider_url = tostring(website.BaseUrl or Config.Media.FiveManage.BaseUrl):gsub("/+$", "")
|
||||
local response = SkyPhoneMediaImport.HttpRequest(
|
||||
("%s/%s"):format(provider_url, SkyPhoneMediaImport.UrlEncode(external_id)),
|
||||
{ ["Authorization"] = api_key(website) },
|
||||
tonumber(website.RequestTimeoutMs or Config.Media.FiveManage.RequestTimeoutMs) or 10000
|
||||
)
|
||||
local decoded, response_error = decode_response(response, "import_media_unavailable")
|
||||
if not decoded then
|
||||
return nil, response_error
|
||||
end
|
||||
|
||||
local file = type(decoded.data) == "table" and decoded.data or decoded
|
||||
if file.id ~= external_id then
|
||||
return nil, "invalid_import_media"
|
||||
end
|
||||
return normalize_file(file)
|
||||
end
|
||||
|
||||
local function probe_public_url(website, url)
|
||||
local timeout = tonumber(website.RequestTimeoutMs or Config.Media.FiveManage.RequestTimeoutMs) or 10000
|
||||
local response = SkyPhoneMediaImport.HttpRequest(url, {}, timeout, "HEAD")
|
||||
local content_type = SkyPhoneMediaImport.ResponseHeader(response.headers, "content-type")
|
||||
local content_length = tonumber(SkyPhoneMediaImport.ResponseHeader(response.headers, "content-length"))
|
||||
|
||||
if response.status == 0 or (response.status >= 200 and response.status < 300
|
||||
and (not content_type or not content_length or content_length <= 0))
|
||||
then
|
||||
Bridge.Debug(
|
||||
"debug",
|
||||
"[sky_phone] FiveManage HEAD probe did not provide usable metadata; trying a one-byte range request."
|
||||
)
|
||||
response = SkyPhoneMediaImport.HttpRequest(url, { ["Range"] = "bytes=0-0" }, timeout)
|
||||
content_type = SkyPhoneMediaImport.ResponseHeader(response.headers, "content-type")
|
||||
local content_range = SkyPhoneMediaImport.ResponseHeader(response.headers, "content-range")
|
||||
content_length = type(content_range) == "string" and tonumber(content_range:match("/(%d+)$"))
|
||||
or tonumber(SkyPhoneMediaImport.ResponseHeader(response.headers, "content-length"))
|
||||
end
|
||||
|
||||
if response.status == 0 then
|
||||
return nil, "import_source_unavailable"
|
||||
end
|
||||
if response.status < 200 or response.status >= 300 then
|
||||
Bridge.Debug(
|
||||
"warn",
|
||||
"[sky_phone] FiveManage public URL probe returned HTTP %s.",
|
||||
tostring(response.status),
|
||||
{ always = true }
|
||||
)
|
||||
return nil, "import_url_unavailable"
|
||||
end
|
||||
|
||||
local normalized_type = media_type(content_type)
|
||||
if not normalized_type then
|
||||
return nil, "import_media_not_allowed"
|
||||
end
|
||||
if not content_length or content_length <= 0 or content_length ~= math.floor(content_length) then
|
||||
return nil, "import_size_unavailable"
|
||||
end
|
||||
|
||||
local url_path = url:match("^https://[^/]+(/[^?#]*)") or ""
|
||||
local external_id = ("url:%08x%08x"):format(
|
||||
joaat(url) & 0xffffffff,
|
||||
joaat("sky_phone:" .. url) & 0xffffffff
|
||||
)
|
||||
return {
|
||||
externalId = external_id,
|
||||
filename = url_path:match("/([^/]+)$") or external_id,
|
||||
mediaType = normalized_type,
|
||||
mimeType = type(content_type) == "string"
|
||||
and content_type:lower():match("^%s*([^;%s]+)") or nil,
|
||||
size = content_length,
|
||||
url = url,
|
||||
}
|
||||
end
|
||||
|
||||
SkyPhoneMediaImport.RegisterAdapter("fivemanage", {
|
||||
Validate = function(website)
|
||||
local url = tostring(website.BaseUrl or Config.Media.FiveManage.BaseUrl):gsub("/+$", "")
|
||||
if not url:match("^https://") or #url > Config.Media.UrlMaxLength then
|
||||
return false, "invalid_base_url"
|
||||
end
|
||||
if type(website.Path) ~= "string" or website.Path == "" or #website.Path > 180 then
|
||||
return false, "missing_import_path"
|
||||
end
|
||||
if api_key(website) == "" then
|
||||
return false, "missing_api_key"
|
||||
end
|
||||
return true
|
||||
end,
|
||||
|
||||
List = function(website, requested_type, page, limit)
|
||||
local provider_type = requested_type == "photo" and "image" or "video"
|
||||
local provider_url = tostring(website.BaseUrl or Config.Media.FiveManage.BaseUrl):gsub("/+$", "")
|
||||
local url = ("%s?page=%s&limit=%s&type=%s&path=%s"):format(
|
||||
provider_url,
|
||||
page,
|
||||
limit,
|
||||
provider_type,
|
||||
SkyPhoneMediaImport.UrlEncode(website.Path)
|
||||
)
|
||||
local response = SkyPhoneMediaImport.HttpRequest(
|
||||
url,
|
||||
{ ["Authorization"] = api_key(website) },
|
||||
tonumber(website.RequestTimeoutMs or Config.Media.FiveManage.RequestTimeoutMs) or 10000
|
||||
)
|
||||
local decoded, response_error = decode_response(response)
|
||||
if not decoded then
|
||||
return nil, response_error
|
||||
end
|
||||
|
||||
local files = type(decoded.data) == "table" and decoded.data or {}
|
||||
local items = {}
|
||||
for _, file in ipairs(files) do
|
||||
items[#items + 1] = normalize_file(file)
|
||||
end
|
||||
local pagination = type(decoded.pagination) == "table" and decoded.pagination or {}
|
||||
local total = math.max(0, math.floor(tonumber(pagination.total) or #items))
|
||||
local current_page = math.max(1, math.floor(tonumber(pagination.page) or page))
|
||||
local page_limit = math.max(1, math.floor(tonumber(pagination.limit) or limit))
|
||||
return {
|
||||
hasMore = current_page * page_limit < total,
|
||||
items = items,
|
||||
total = total,
|
||||
}
|
||||
end,
|
||||
|
||||
Resolve = resolve_file,
|
||||
|
||||
ResolveUrl = function(website, url)
|
||||
local external_id = public_file_id(url)
|
||||
if external_id then
|
||||
local file, resolve_error = resolve_file(website, external_id)
|
||||
if file then
|
||||
Bridge.Debug(
|
||||
"debug",
|
||||
"[sky_phone] FiveManage URL resolved through authenticated metadata for file '%s'.",
|
||||
external_id
|
||||
)
|
||||
return file
|
||||
end
|
||||
if resolve_error == "import_provider_unauthorized" then
|
||||
return nil, resolve_error
|
||||
end
|
||||
Bridge.Debug(
|
||||
"debug",
|
||||
"[sky_phone] FiveManage metadata lookup for file '%s' failed with '%s'; probing the public URL.",
|
||||
external_id,
|
||||
tostring(resolve_error)
|
||||
)
|
||||
end
|
||||
return probe_public_url(website, url)
|
||||
end,
|
||||
})
|
||||
@@ -0,0 +1,153 @@
|
||||
local manifest_cache = {}
|
||||
|
||||
local function authentication_headers(website)
|
||||
local auth = website.Auth
|
||||
if not auth or auth.Type == nil or auth.Type == "none" then
|
||||
return {}
|
||||
end
|
||||
|
||||
if auth.Type == "bearer" then
|
||||
return { ["Authorization"] = "Bearer " .. GetConvar(auth.TokenConvar, "") }
|
||||
end
|
||||
return { [auth.Header] = GetConvar(auth.ValueConvar, "") }
|
||||
end
|
||||
|
||||
local function validate_authentication(auth)
|
||||
if auth == nil then
|
||||
return true
|
||||
end
|
||||
if type(auth) ~= "table" then
|
||||
return false, "invalid_auth"
|
||||
end
|
||||
if auth.Type == nil or auth.Type == "none" then
|
||||
return true
|
||||
end
|
||||
if auth.Type == "bearer" then
|
||||
if type(auth.TokenConvar) ~= "string" or auth.TokenConvar == ""
|
||||
or GetConvar(auth.TokenConvar, "") == ""
|
||||
then
|
||||
return false, "missing_auth_convar"
|
||||
end
|
||||
return true
|
||||
end
|
||||
if auth.Type == "header" then
|
||||
if type(auth.Header) ~= "string" or not auth.Header:match("^[%w%-]+$")
|
||||
or type(auth.ValueConvar) ~= "string" or auth.ValueConvar == ""
|
||||
or GetConvar(auth.ValueConvar, "") == ""
|
||||
then
|
||||
return false, "invalid_header_auth"
|
||||
end
|
||||
return true
|
||||
end
|
||||
return false, "unknown_auth_type"
|
||||
end
|
||||
|
||||
local function fetch_manifest(website)
|
||||
local now = os.time()
|
||||
local cached = manifest_cache[website.Id]
|
||||
if cached and cached.expires_at > now then
|
||||
return cached.items
|
||||
end
|
||||
|
||||
local response = SkyPhoneMediaImport.HttpRequest(
|
||||
website.ManifestUrl,
|
||||
authentication_headers(website),
|
||||
tonumber(website.RequestTimeoutMs) or 10000
|
||||
)
|
||||
if response.status == 0 then
|
||||
return nil, "import_source_unavailable"
|
||||
end
|
||||
if response.status == 401 or response.status == 403 then
|
||||
return nil, "import_provider_unauthorized"
|
||||
end
|
||||
if response.status < 200 or response.status >= 300 then
|
||||
return nil, "import_provider_failed"
|
||||
end
|
||||
|
||||
local max_bytes = math.max(1024, math.floor(tonumber(Config.Media.Import.ManifestMaxBytes) or 2097152))
|
||||
if #response.body > max_bytes then
|
||||
return nil, "import_provider_failed"
|
||||
end
|
||||
|
||||
local success, decoded = pcall(json.decode, response.body)
|
||||
if not success or type(decoded) ~= "table" or tonumber(decoded.version) ~= 1
|
||||
or type(decoded.items) ~= "table"
|
||||
then
|
||||
return nil, "import_provider_failed"
|
||||
end
|
||||
|
||||
local maximum_items = math.max(1, math.floor(tonumber(Config.Media.Import.ManifestMaxItems) or 5000))
|
||||
if #decoded.items > maximum_items then
|
||||
return nil, "import_provider_failed"
|
||||
end
|
||||
|
||||
local items = {}
|
||||
for _, item in ipairs(decoded.items) do
|
||||
if type(item) == "table" then
|
||||
items[#items + 1] = {
|
||||
externalId = item.id,
|
||||
filename = item.filename,
|
||||
mediaType = item.type,
|
||||
mimeType = item.mimeType,
|
||||
size = item.size,
|
||||
url = item.url,
|
||||
}
|
||||
end
|
||||
end
|
||||
manifest_cache[website.Id] = {
|
||||
expires_at = now + math.max(1, math.floor(tonumber(website.CacheSeconds)
|
||||
or tonumber(Config.Media.Import.ManifestCacheSeconds) or 30)),
|
||||
items = items,
|
||||
}
|
||||
return items
|
||||
end
|
||||
|
||||
SkyPhoneMediaImport.RegisterAdapter("manifest", {
|
||||
Validate = function(website)
|
||||
if type(website.ManifestUrl) ~= "string"
|
||||
or not website.ManifestUrl:match("^https://")
|
||||
or #website.ManifestUrl > Config.Media.UrlMaxLength
|
||||
then
|
||||
return false, "invalid_manifest_url"
|
||||
end
|
||||
return validate_authentication(website.Auth)
|
||||
end,
|
||||
|
||||
List = function(website, requested_type, page, limit)
|
||||
local manifest, manifest_error = fetch_manifest(website)
|
||||
if not manifest then
|
||||
return nil, manifest_error
|
||||
end
|
||||
|
||||
local matching = {}
|
||||
for _, item in ipairs(manifest) do
|
||||
if item.mediaType == requested_type then
|
||||
matching[#matching + 1] = item
|
||||
end
|
||||
end
|
||||
local first = (page - 1) * limit + 1
|
||||
local last = math.min(#matching, first + limit - 1)
|
||||
local items = {}
|
||||
for index = first, last do
|
||||
items[#items + 1] = matching[index]
|
||||
end
|
||||
return {
|
||||
hasMore = last < #matching,
|
||||
items = items,
|
||||
total = #matching,
|
||||
}
|
||||
end,
|
||||
|
||||
Resolve = function(website, external_id)
|
||||
local manifest, manifest_error = fetch_manifest(website)
|
||||
if not manifest then
|
||||
return nil, manifest_error
|
||||
end
|
||||
for _, item in ipairs(manifest) do
|
||||
if item.externalId == external_id then
|
||||
return item
|
||||
end
|
||||
end
|
||||
return nil, "import_media_unavailable"
|
||||
end,
|
||||
})
|
||||
@@ -153,8 +153,14 @@ CREATE TABLE IF NOT EXISTS `sky_phone_media` (
|
||||
`url` TEXT NOT NULL,
|
||||
`remote_id` VARCHAR(128) NOT NULL,
|
||||
`media_type` ENUM('photo', 'video') NOT NULL,
|
||||
`mime_type` VARCHAR(120) NULL,
|
||||
`origin` ENUM('phone_upload', 'website_import') NOT NULL DEFAULT 'phone_upload',
|
||||
`source_id` VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL,
|
||||
`verified_at` DATETIME NULL,
|
||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uniq_sky_phone_media_account_source` (`account_id`, `source_id`, `remote_id`, `origin`),
|
||||
UNIQUE KEY `uniq_sky_phone_media_device_source` (`device_imei`, `source_id`, `remote_id`, `origin`),
|
||||
KEY `idx_sky_phone_media_account` (`account_id`, `created_at`, `id`),
|
||||
KEY `idx_sky_phone_media_device` (`device_imei`, `created_at`, `id`),
|
||||
FOREIGN KEY (`account_id`) REFERENCES `sky_phone_accounts` (`id`) ON DELETE CASCADE,
|
||||
|
||||
Reference in New Issue
Block a user