ustore.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/usr/bin/ruby
  2. #
  3. # This file is part of centurio.work/ing/commands.
  4. #
  5. # centurio.work/ing/commands is free software: you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or (at your
  8. # option) any later version.
  9. #
  10. # centurio.work/ing/commands is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  13. # Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along with
  16. # centurio.work/ing/commands (file COPYING in the main directory). If not, see
  17. # <http://www.gnu.org/licenses/>.
  18. require 'rubygems'
  19. require 'json'
  20. require 'riddl/server'
  21. require 'riddl/client'
  22. require 'fileutils'
  23. require 'nokogiri'
  24. class GetFolders < Riddl::Implementation
  25. def response
  26. Dir.chdir( __dir__ + '/../storage')
  27. i = 1
  28. while i < @r.length do
  29. Dir.chdir(@r[i])
  30. i +=1
  31. end
  32. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(Dir.glob('*/')))
  33. end
  34. end
  35. class CreateFolder < Riddl::Implementation
  36. def response
  37. path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  38. FileUtils.mkdir_p(path)
  39. end
  40. end
  41. class DeleteFolder < Riddl::Implementation
  42. def response
  43. path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  44. FileUtils.rm_rf(path)
  45. end
  46. end
  47. class GetAllFolders < Riddl::Implementation
  48. def response
  49. Dir.chdir( __dir__ + '/../storage')
  50. i = 1
  51. while i < @r.length do
  52. Dir.chdir(@r[i])
  53. i +=1
  54. end
  55. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(Dir.glob('**/*/')))
  56. end
  57. end
  58. class GetImages < Riddl::Implementation
  59. def response
  60. Dir.chdir( __dir__ + '/../storage')
  61. i = 1
  62. while i < @r.length do
  63. Dir.chdir(@r[i])
  64. i +=1
  65. end
  66. ret = [];
  67. Dir.glob('*').select{ |e|
  68. if File.file? e
  69. ret.append(e)
  70. end
  71. }
  72. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  73. end
  74. end
  75. class UploadData < Riddl::Implementation
  76. def response
  77. i = 0
  78. while i < @p.length do
  79. item = @p[i]
  80. if(item != nil && item.name == "files[]")
  81. # puts item.inspect
  82. path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  83. FileUtils.mkdir_p(path)
  84. readFile = File.read(item.value.inspect.to_s[/Tempfile:(.*?)>/m, 1])
  85. File.open(File.join(path, item.filename), 'wb') do |file|
  86. file.write(readFile.to_s)
  87. end
  88. end
  89. i +=1
  90. end
  91. end
  92. end
  93. class DeleteData < Riddl::Implementation
  94. def response
  95. datalink = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  96. puts datalink
  97. File.delete(datalink) if File.exist?(datalink)
  98. end
  99. end
  100. class NewExternalFolder < Riddl::Implementation
  101. def response
  102. parsed = JSON.parse(@p[0].value.read)
  103. found = false;
  104. path = File.join(File.dirname(__dir__),'server/ustore_Folders.txt')
  105. text = File::readlines(path)
  106. text.each do |line|
  107. if line.chomp == parsed["folder"]
  108. found = true
  109. end
  110. end
  111. if !found
  112. File.open(path, 'a') do |file|
  113. file.puts parsed["folder"]
  114. end
  115. end
  116. end
  117. end
  118. class GetAllExternalFolders < Riddl::Implementation
  119. def response
  120. path = File.join(File.dirname(__dir__),'server/ustore_Folders.txt')
  121. text = File::readlines(path)
  122. text.each(&:chomp)
  123. puts text
  124. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(text))
  125. end
  126. end
  127. server = Riddl::Server.new(File.join(__dir__,'/ustore.xml'), :host => 'localhost') do |opts|
  128. accessible_description true
  129. cross_site_xhr true
  130. on resource do
  131. on resource 'folders' do
  132. run GetFolders if get
  133. run CreateFolder if post
  134. run DeleteFolder if delete
  135. on resource '.*' do
  136. run GetFolders if get
  137. run CreateFolder if post
  138. run DeleteFolder if delete
  139. end
  140. end
  141. on resource 'allfolders' do
  142. run GetAllFolders if get
  143. on resource '.*' do
  144. run GetAllFolders if get
  145. end
  146. end
  147. on resource 'images' do
  148. run GetImages if get
  149. on resource '.*' do
  150. run GetImages if get
  151. end
  152. end
  153. on resource 'data' do
  154. run UploadData if post
  155. run DeleteData if delete
  156. on resource '.*' do
  157. run UploadData if post
  158. run DeleteData if delete
  159. end
  160. end
  161. on resource 'externalFolder' do
  162. run NewExternalFolder if post
  163. run GetAllExternalFolders if get
  164. end
  165. end
  166. end.loop!