Module:Image

From Granblue Fantasy Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:Image/doc

local p = {}

function p.fetchPath(frame, image, thumb)
	local search_title
	if frame.args.image ~= nil then
		search_title = mw.text.decode(frame.args.image, true)
	else
		search_title = image
	end
	local thumb_size
	if frame.args.size ~= nil then
		thumb_size = frame.args.size
	else
		thumb_size = thumb
	end
	if (thumb_size ~= nil) and (string.match(thumb_size, '^%d+px$') == nil) then
		error('thumb_size value not valid', 1)
	end
		

	local title = p.fetchFinalTitle(mw.title.makeTitle('Image', search_title))
	if title == nil then
		return ''
	end
	
	--mw.log(title)
	local filename = title:partialUrl()
	--mw.log(filename)
	local hash_path = p.calcHashPath(filename)
	--mw.log(md5)
	if thumb ~= nil then
		local encoded_filename = mw.uri.encode(filename, 'WIKI')
		return '/images/thumb/' .. hash_path .. '/' .. encoded_filename .. '/' .. thumb_size .. '-' .. encoded_filename;
	else
		return '/images/' .. hash_path .. '/' .. mw.uri.encode(filename, 'WIKI');
	end
end

function p.fetchURL(frame, image, thumb)
	return 'https://gbf.wiki' .. p.fetchPath(frame, image, thumb)
end

function p.calcHashPath(filename)
	local md5 = mw.hash.hashValue('md5', filename)
	return string.sub(md5, 1, 1) .. '/' .. string.sub(md5, 1, 2)
end

function p.fetchFinalTitle(title)
	if title == nil then
		return nil
	elseif title.redirectTarget == false then
		return title
	else
		return p.fetchFinalTitle(title.redirectTarget)
	end
end

function p.test() 
	local frame = mw.getCurrentFrame()
	mw.logObject(p.fetchURL(frame, 'EugenSummer_iconA.jpg', nil))
end

function p.test2() 
	local frame = mw.getCurrentFrame()
	mw.logObject(p.fetchURL(frame, 'EugenSummer_iconA.jpg', '2px'))
end

function p.test3() 
	local frame = mw.getCurrentFrame()
	mw.logObject(p.fetchPath(frame, 'Minami_Nitta_iconA.jpg', '70px'))
end

function p.test4()
	local frame = mw.getCurrentFrame()
	frame.args.image = "Jeanne d'Arc (Themed) iconA.jpg"
	mw.logObject(p.fetchURL(frame, nil, nil))
end

return p