Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow uploading/downloading to norns.online #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cheat_codes_2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ rnd = include 'lib/rnd_actions'
del = include 'lib/delay'
rytm = include 'lib/euclid'
mc = include 'lib/midicheat'
sharer = include 'lib/sharer'
math.randomseed(os.time())
variable_fade_time = 0.01

Expand Down Expand Up @@ -721,7 +722,7 @@ zilch_leds =
}

function init()

sharer.setup("cheat_codes_2")
clock.run(check_page_for_k1)

collection_loaded = false
Expand Down
153 changes: 153 additions & 0 deletions lib/sharer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
local sharer={}

local CCDATA_DIR=_path.data.."cheat_codes_2/"

function sharer.setup(script_name)
-- only continue if norns.online exists
if not util.file_exists(_path.code.."norns.online") then
print("need to donwload norns.online")
do return end
end

-- prevents initial bang for reloading directory
preventbang=true
clock.run(function()
clock.sleep(2)
preventbang=false
end)

-- load norns.online lib
local share=include("norns.online/lib/share")

-- start uploader with name of your script
local uploader=share:new{script_name=script_name}
if uploader==nil then
print("uploader failed, no username?")
do return end
end

-- add parameters
params:add_group("SHARE",4)

-- uploader (CHANGE THIS TO FIT WHAT YOU NEED)
-- select a save from the names folder
local names_dir=CCDATA_DIR.."names/"
params:add_file("share_upload","upload",names_dir)
params:set_action("share_upload",function(y)
-- prevent banging
local x=y
params:set("share_download",names_dir)
if #x<=#names_dir then
do return end
end

-- choose data name
-- (here dataname is from the selector)
local dataname=share.trim_prefix(x,CCDATA_DIR.."names/")
dataname=dataname:gsub("%.cc2","") -- remove suffix
params:set("share_message","uploading...")
_menu.redraw()
print("uploading "..x.." as "..dataname)

-- get all of the files in the collection and upload them
files=sharer.list_files(CCDATA_DIR.."collection-"..dataname.."/",{},true)
for i,f in ipairs(files) do
partial_path=sharer.trim_prefix(f,CCDATA_DIR.."collection-"..dataname.."/")
target=CCDATA_DIR.."collection-"..uploader.upload_username.."_"..dataname.."/"..partial_path
pathtofile=f
msg=uploader:upload{dataname=dataname,pathtofile=pathtofile,target=target}
if not string.match(msg,"OK") then
params:set("share_message",msg)
do return end
end
end

-- create and upload the names file
tmp_file=share.temp_file_name()
local f=io.open(tmp_file,"w")
f:write(uploader.upload_username.."_"..dataname)
f:close()
pathtofile=tmp_file
target=CCDATA_DIR.."names/"..uploader.upload_username.."_"..dataname..".cc2"
uploader:upload{dataname=dataname,pathtofile=pathtofile,target=target}
os.remove(tmp_file)

-- goodbye
params:set("share_message","uploaded.")
end)

-- downloader
download_dir=share.get_virtual_directory(script_name)
params:add_file("share_download","download",download_dir)
params:set_action("share_download",function(y)
-- prevent banging
local x=y
params:set("share_download",download_dir)
if #x<=#download_dir then
do return end
end

-- download
print("downloading!")
params:set("share_message","downloading...")
_menu.redraw()
local msg=share.download_from_virtual_directory(x)
params:set("share_message",msg)
end)

-- add a button to refresh the directory
params:add{type='binary',name='refresh directory',id='share_refresh',behavior='momentary',action=function(v)
if preventbang then
do return end
end
print("updating directory")
params:set("share_message","refreshing directory.")
_menu.redraw()
share.make_virtual_directory()
params:set("share_message","directory updated.")
end
}
params:add_text('share_message',">","")
end



function sharer.list_files(d,files,recursive)
-- list files in a flat table
if d=="." or d=="./" then
d=""
end
if d~="" and string.sub(d,-1)~="/" then
d=d.."/"
end
folders={}
if recursive then
local cmd="ls -ad "..d.."*/ 2>/dev/null"
local f=assert(io.popen(cmd,'r'))
local out=assert(f:read('*a'))
f:close()
for s in out:gmatch("%S+") do
if not (string.match(s,"ls: ") or s=="../" or s=="./") then
files=sharer.list_files(s,files,recursive)
end
end
end
do
local cmd="ls -p "..d.." | grep -v /"
local f=assert(io.popen(cmd,'r'))
local out=assert(f:read('*a'))
f:close()
for s in out:gmatch("%S+") do
table.insert(files,d..s)
end
end
return files
end

sharer.trim_prefix=function(s,p)
local t=(s:sub(0,#p)==p) and s:sub(#p+1) or s
return t
end


return sharer