ustore.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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('*/').sort_by{|x| x.downcase}))
  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('**/*/').sort_by{|x| x.downcase}))
  56. end
  57. end
  58. class GetImages < Riddl::Implementation
  59. def response
  60. selectElements = @p[0];
  61. if(selectElements.nil? || selectElements == "")
  62. selectElements = 100;
  63. else
  64. selectElements = @p[0].value.to_i;
  65. end
  66. pp selectElements
  67. Dir.chdir( __dir__ + '/../storage')
  68. i = 1
  69. while i < @r.length do
  70. Dir.chdir(@r[i])
  71. i +=1
  72. end
  73. ret = [];
  74. ret2 = [];
  75. i = 0
  76. #Sort for Date (newest first)
  77. files_sorted_by_time = Dir['*'].sort_by{ |f| File.mtime(f) }.reverse
  78. files_sorted_by_time.select{ |e|
  79. if(i > selectElements)
  80. break
  81. end
  82. if File.symlink?(e)
  83. ret2.append(e)
  84. elsif( File.file?(e))
  85. ret.append(e)
  86. end
  87. i = i +1
  88. }
  89. ret = ret.sort_by{|x| x.downcase}
  90. ret2 = ret2.sort_by{|x| x.downcase}
  91. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret + ret2))
  92. end
  93. end
  94. class UploadData < Riddl::Implementation
  95. def response
  96. i = 0
  97. while i < @p.length do
  98. item = @p[i]
  99. if(item != nil && item.name == "files[]")
  100. #puts item.inspect
  101. #get filename from additional info as .filname is not alwys right?
  102. readFilename = item.additional.inspect.to_s[/filename=\\"(.*?)\\"/m, 1]
  103. path = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  104. FileUtils.mkdir_p(path)
  105. readFile = File.read(item.value.inspect.to_s[/Tempfile:(.*?)>/m, 1])
  106. File.open(File.join(path, readFilename), 'wb') do |file|
  107. file.write(readFile.to_s)
  108. end
  109. end
  110. i +=1
  111. end
  112. end
  113. end
  114. class CreateSymlink < Riddl::Implementation
  115. def response
  116. datalink = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  117. datalink2 = datalink
  118. data = JSON.parse(@p[0].value.read)
  119. #replace last oldfilename with new one
  120. datalink2 = datalink.reverse.sub(data["oldfilename"].reverse, data["filename"].reverse).reverse
  121. File.symlink(datalink, datalink2)
  122. end
  123. end
  124. class RenameFile < Riddl::Implementation
  125. def response
  126. datalink = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  127. datalink2 = datalink
  128. data = JSON.parse(@p[0].value.read)
  129. #replace last oldfilename with new one
  130. datalink2 = datalink.reverse.sub(data["oldfilename"].reverse, data["newfilename"].reverse).reverse
  131. File.rename(datalink, datalink2)
  132. end
  133. end
  134. class DeleteData < Riddl::Implementation
  135. def response
  136. datalink = File.join(File.dirname(__dir__),'storage/', @r.drop(1).join("/"))
  137. puts datalink
  138. File.delete(datalink) if File.exist?(datalink)
  139. File.delete(datalink) if File.symlink?(datalink)
  140. end
  141. end
  142. class NewExternalFolder < Riddl::Implementation
  143. def response
  144. parsed = JSON.parse(@p[0].value.read)
  145. found = false;
  146. path = File.join(File.dirname(__dir__),'server/ustore_Folders.txt')
  147. text = File::readlines(path)
  148. text.each do |line|
  149. if line.chomp == parsed["folder"]
  150. found = true
  151. end
  152. end
  153. if !found
  154. File.open(path, 'a') do |file|
  155. file.puts parsed["folder"]
  156. end
  157. end
  158. end
  159. end
  160. class GetAllExternalFolders < Riddl::Implementation
  161. def response
  162. path = File.join(File.dirname(__dir__),'server/ustore_Folders.txt')
  163. text = File::readlines(path)
  164. text.each(&:chomp)
  165. puts text
  166. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(text))
  167. end
  168. end
  169. server = Riddl::Server.new(File.join(__dir__,'/ustore.xml'), :host => 'localhost') do |opts|
  170. accessible_description true
  171. cross_site_xhr true
  172. on resource do
  173. on resource 'folders' do
  174. run GetFolders if get
  175. run CreateFolder if post
  176. run DeleteFolder if delete
  177. on resource '.*' do
  178. run GetFolders if get
  179. run CreateFolder if post
  180. run DeleteFolder if delete
  181. end
  182. end
  183. on resource 'allfolders' do
  184. run GetAllFolders if get
  185. on resource '.*' do
  186. run GetAllFolders if get
  187. end
  188. end
  189. on resource 'images' do
  190. run GetImages if get
  191. on resource '.*' do
  192. run GetImages if get
  193. end
  194. end
  195. on resource 'data' do
  196. run UploadData if post
  197. run DeleteData if delete
  198. run CreateSymlink if post 'list'
  199. run RenameFile if put 'list'
  200. on resource '.*' do
  201. run UploadData if post
  202. run CreateSymlink if post 'list'
  203. run RenameFile if put 'list'
  204. run DeleteData if delete
  205. end
  206. end
  207. on resource 'externalFolder' do
  208. run NewExternalFolder if post
  209. run GetAllExternalFolders if get
  210. end
  211. end
  212. end.loop!