github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/tests/system/lib/cozy_file.rb (about) 1 # File is already taken by the stdlib 2 class CozyFile 3 include Model::Files 4 5 attr_reader :name, :dir_id, :mime, :trashed, :md5sum, :referenced_by, 6 :metadata, :size, :executable, :file_class, :cozy_metadata, 7 :old_versions 8 9 def self.parse_jsonapi(body) 10 j = JSON.parse(body)["data"] 11 id = j["id"] 12 rev = j.dig "meta", "rev" 13 old_versions = j.dig "relationships", "old_versions", "data" 14 referenced_by = j.dig "relationships", "referenced_by", "data" 15 j = j["attributes"] 16 f = CozyFile.new( 17 name: j["name"], 18 dir_id: j["dir_id"], 19 trashed: j["trashed"], 20 md5sum: j["md5sum"], 21 size: j["size"], 22 executable: j["executable"], 23 mime: j["mime"], 24 file_class: j["class"], 25 metadata: j["metadata"], 26 cozy_metadata: j["cozyMetadata"], 27 old_versions: old_versions, 28 referenced_by: referenced_by 29 ) 30 f.couch_id = id 31 f.couch_rev = rev 32 f 33 end 34 35 def self.load_from_url(inst, path) 36 opts = { 37 accept: "application/vnd.api+json", 38 authorization: "Bearer #{inst.token_for doctype}" 39 } 40 res = inst.client[path].get opts 41 parse_jsonapi res.body 42 end 43 44 def restore_from_trash(inst) 45 opts = { 46 authorization: "Bearer #{inst.token_for doctype}" 47 } 48 res = inst.client["/files/trash/#{@couch_id}"].post nil, opts 49 j = JSON.parse(res.body)["data"] 50 @trashed = false 51 @dir_id = j.dig "attributes", "dir_id" 52 end 53 54 def self.options_from_fixture(filename, opts = {}) 55 opts = opts.dup 56 opts[:content] = File.read filename 57 opts[:name] ||= "#{Faker::Internet.slug}#{File.extname(filename)}" 58 opts[:mime] ||= MiniMime.lookup_by_filename(filename).content_type 59 opts 60 end 61 62 def self.metadata_options_for(inst, meta) 63 opts = { 64 authorization: "Bearer #{inst.token_for doctype}", 65 :"content-type" => "application/vnd.api+json" 66 } 67 body = JSON.generate data: { type: "io.cozy.files.metadata", attributes: meta } 68 res = inst.client["/files/upload/metadata"].post body, opts 69 id = JSON.parse(res.body)["data"]["id"] 70 { metadata_id: id } 71 end 72 73 def initialize(opts = {}) 74 @name = opts[:name] || Faker::Internet.slug 75 @dir_id = opts[:dir_id] || Folder::ROOT_DIR 76 @mime = opts[:mime] || "text/plain" 77 @content = opts[:content] || Faker::TvShows::Friends.quote 78 @trashed = opts[:trashed] 79 @md5sum = opts[:md5sum] 80 @metadata = opts[:metadata] 81 @size = opts[:size] 82 @executable = opts[:executable] 83 @file_class = opts[:file_class] 84 @cozy_metadata = opts[:cozy_metadata] 85 @referenced_by = opts[:referenced_by] 86 @old_versions = opts[:old_versions] || [] 87 end 88 89 def save(inst) 90 opts = { 91 accept: "application/vnd.api+json", 92 authorization: "Bearer #{inst.token_for doctype}", 93 :"content-type" => mime 94 } 95 res = inst.client["/files/#{dir_id}?Type=file&Name=#{CGI.escape name}"].post @content, opts 96 j = JSON.parse(res.body)["data"] 97 @couch_id = j["id"] 98 @couch_rev = j["meta"]["rev"] 99 @cozy_metadata = j["attributes"]["cozyMetadata"] 100 end 101 102 def overwrite(inst, opts = {}) 103 @mime = opts[:mime] || @mime 104 @content = opts[:content] || Faker::TvShows::Friends.quote 105 headers = { 106 accept: "application/vnd.api+json", 107 authorization: "Bearer #{inst.token_for doctype}", 108 :"content-type" => mime 109 } 110 u = "/files/#{@couch_id}" 111 u = "#{u}?MetadataID=#{opts[:metadata_id]}" if opts[:metadata_id] 112 res = inst.client[u].put @content, headers 113 j = JSON.parse(res.body)["data"] 114 @couch_rev = j["meta"]["rev"] 115 @md5sum = j["attributes"]["md5sum"] 116 end 117 118 PHOTOS = %w(about apps architecture business community faq features try).freeze 119 120 def self.create_photos(inst, opts = {}) 121 dir = File.expand_path "../.photos", Helpers.current_dir 122 PHOTOS.map do |photo| 123 create inst, options_from_fixture("#{dir}/#{photo}.jpg", opts) 124 end 125 end 126 127 def self.ensure_photos_in_cache 128 dir = File.expand_path "../.photos", Helpers.current_dir 129 return if Dir.exist? dir 130 FileUtils.mkdir_p dir 131 PHOTOS.each do |photo| 132 url = "https://cozy.io/fr/images/bkg-#{photo}.jpg" 133 `wget -q #{url} -O #{dir}/#{photo}.jpg` 134 end 135 end 136 end