#!/usr/bin/ruby # # This file is part of centurio.work/ing/commands. # # centurio.work/ing/commands is free software: you can redistribute it and/or # modify it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or (at your # option) any later version. # # centurio.work/ing/commands is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. # # You should have received a copy of the GNU General Public License along with # centurio.work/ing/commands (file COPYING in the main directory). If not, see # . require 'rubygems' require 'json' require 'riddl/server' require 'riddl/client' require 'fileutils' require 'nokogiri' class GetFolders < Riddl::Implementation def response Dir.chdir( __dir__ + '/../storage') i = 1 while i < @r.length do Dir.chdir(@r[i]) i +=1 end Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(Dir.glob('*/').sort_by{|x| x.downcase})) end end class CreateFolder < Riddl::Implementation def response path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/")) FileUtils.mkdir_p(path) end end class DeleteFolder < Riddl::Implementation def response path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/")) FileUtils.rm_rf(path) end end class GetAllFolders < Riddl::Implementation def response Dir.chdir( __dir__ + '/../storage') i = 1 while i < @r.length do Dir.chdir(@r[i]) i +=1 end Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(Dir.glob('**/*/').sort_by{|x| x.downcase})) end end class GetImages < Riddl::Implementation def response Dir.chdir( __dir__ + '/../storage') i = 1 while i < @r.length do Dir.chdir(@r[i]) i +=1 end ret = []; ret2 = []; Dir.glob('*').select{ |e| if File.symlink?(e) ret2.append(e) elsif( File.file?(e)) ret.append(e) end } ret = ret.sort_by{|x| x.downcase} ret2 = ret2.sort_by{|x| x.downcase} Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret + ret2)) end end class UploadData < Riddl::Implementation def response i = 0 while i < @p.length do item = @p[i] if(item != nil && item.name == "files[]") # puts item.inspect path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/")) FileUtils.mkdir_p(path) readFile = File.read(item.value.inspect.to_s[/Tempfile:(.*?)>/m, 1]) File.open(File.join(path, item.filename), 'wb') do |file| file.write(readFile.to_s) end end i +=1 end end end class CreateSymlink < Riddl::Implementation def response datalink = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/")) datalink2 = datalink data = JSON.parse(@p[0].value.read) #replace last oldfilename with new one datalink2 = datalink.reverse.sub(data["oldfilename"].reverse, data["filename"].reverse).reverse #puts datalink #puts datalink2 File.symlink(datalink, datalink2) end end class DeleteData < Riddl::Implementation def response datalink = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/")) puts datalink File.delete(datalink) if File.exist?(datalink) File.delete(datalink) if File.symlink?(datalink) end end class NewExternalFolder < Riddl::Implementation def response parsed = JSON.parse(@p[0].value.read) found = false; path = File.join(File.dirname(__dir__),'server/ustore_Folders.txt') text = File::readlines(path) text.each do |line| if line.chomp == parsed["folder"] found = true end end if !found File.open(path, 'a') do |file| file.puts parsed["folder"] end end end end class GetAllExternalFolders < Riddl::Implementation def response path = File.join(File.dirname(__dir__),'server/ustore_Folders.txt') text = File::readlines(path) text.each(&:chomp) puts text Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(text)) end end server = Riddl::Server.new(File.join(__dir__,'/ustore.xml'), :host => 'localhost') do |opts| accessible_description true cross_site_xhr true on resource do on resource 'folders' do run GetFolders if get run CreateFolder if post run DeleteFolder if delete on resource '.*' do run GetFolders if get run CreateFolder if post run DeleteFolder if delete end end on resource 'allfolders' do run GetAllFolders if get on resource '.*' do run GetAllFolders if get end end on resource 'images' do run GetImages if get on resource '.*' do run GetImages if get end end on resource 'data' do run UploadData if post run DeleteData if delete run CreateSymlink if post 'list' on resource '.*' do run UploadData if post run CreateSymlink if post 'list' run DeleteData if delete end end on resource 'externalFolder' do run NewExternalFolder if post run GetAllExternalFolders if get end end end.loop!