123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- #!/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
- # <http://www.gnu.org/licenses/>.
- 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
- selectElements = @p[0];
-
- if(selectElements.nil? || selectElements == "")
- selectElements = 100;
- else
- selectElements = @p[0].value.to_i;
- end
-
- pp selectElements
-
- Dir.chdir( __dir__ + '/../storage')
- i = 1
- while i < @r.length do
- Dir.chdir(@r[i])
- i +=1
- end
-
- ret = [];
- ret2 = [];
- i = 0
-
- #Sort for Date (newest first)
- files_sorted_by_time = Dir['*'].sort_by{ |f| File.mtime(f) }.reverse
-
- files_sorted_by_time.select{ |e|
- if(i > selectElements)
- break
- end
- if File.symlink?(e)
- ret2.append(e)
- elsif( File.file?(e))
- ret.append(e)
- end
- i = i +1
- }
- 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
-
- #get filename from additional info as .filname is not alwys right?
- readFilename = item.additional.inspect.to_s[/filename=\\"(.*?)\\"/m, 1]
-
- 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, readFilename), '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
- File.symlink(datalink, datalink2)
- end
- end
- class RenameFile < 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["newfilename"].reverse).reverse
-
- File.rename(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'
- run RenameFile if put 'list'
- on resource '.*' do
- run UploadData if post
- run CreateSymlink if post 'list'
- run RenameFile if put 'list'
- run DeleteData if delete
- end
- end
-
- on resource 'externalFolder' do
- run NewExternalFolder if post
- run GetAllExternalFolders if get
- end
-
-
- end
- end.loop!
|