Class: Texd::Attachment::Reference

Inherits:
Base
  • Object
show all
Defined in:
lib/texd/attachment.rb

Constant Summary collapse

USE_REF =

Special Content-Type header to instruct texd server to interpret contents as reference identifier and re-use persisted file on server.

"application/x.texd; ref=use"
STORE_REF =

Special Content-Type header to instruct texd server to store the content body for later reference.

"application/x.texd; ref=store"

Instance Attribute Summary

Attributes inherited from Base

#absolute_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #name

Constructor Details

This class inherits a constructor from Texd::Attachment::Base

Class Method Details

.cacheObject



247
248
249
# File 'lib/texd/attachment.rb', line 247

def self.cache
  @cache ||= Cache.new(Texd.config.ref_cache_size)
end

Instance Method Details

#checksumObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



252
253
254
# File 'lib/texd/attachment.rb', line 252

def checksum
  @checksum ||= create_checksum
end

#create_checksumObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



257
258
259
260
261
262
263
264
# File 'lib/texd/attachment.rb', line 257

def create_checksum
  key = [::File.stat(absolute_path).mtime, absolute_path]
  self.class.cache.fetch(key) do
    digest  = Digest::SHA256.file(absolute_path).digest
    encoded = Base64.urlsafe_encode64(digest)
    "sha256:#{encoded}"
  end
end

#to_upload_io(full: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



238
239
240
241
242
243
244
245
# File 'lib/texd/attachment.rb', line 238

def to_upload_io(full: false)
  f  = full ? absolute_path.open("rb") : StringIO.new(checksum)
  ct = full ? STORE_REF                : USE_REF

  Multipart::Post::UploadIO.new(f, ct, name).tap { |io|
    io.instance_variable_set :@original_filename, name
  }
end