Module: TiktokBusinessApi::Utils

Defined in:
lib/tiktok_business_api/utils.rb

Overview

Utility methods for TikTok Business API

Class Method Summary collapse

Class Method Details

.calculate_md5(file) ⇒ String

Calculate MD5 hash of the file

Parameters:

  • file (File, String)

    File object or file path

Returns:

  • (String)

    MD5 hash



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tiktok_business_api/utils.rb', line 11

def self.calculate_md5(file)
  if file.is_a?(String) && File.exist?(file)
    # File path was provided
    Digest::MD5.file(file).hexdigest
  elsif file.respond_to?(:path) && File.exist?(file.path)
    # File object with path
    Digest::MD5.file(file.path).hexdigest
  elsif file.respond_to?(:read)
    # IO-like object
    content = file.read
    file.rewind if file.respond_to?(:rewind) # Reset the file pointer
    Digest::MD5.hexdigest(content)
  else
    raise ArgumentError, "Unable to calculate MD5: invalid file object"
  end
end

.detect_content_type(file) ⇒ String

Detect content type based on file extension

Parameters:

  • file (File, String)

    File object or file path

Returns:

  • (String)

    MIME type



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tiktok_business_api/utils.rb', line 32

def self.detect_content_type(file)
  file_path = if file.is_a?(String)
                file
              elsif file.respond_to?(:path)
                file.path
              else
                return "application/octet-stream" # Default if we can't determine
              end

  # Simple extension to MIME type mapping for common image formats
  case File.extname(file_path).downcase
  when '.jpg', '.jpeg'
    'image/jpeg'
  when '.png'
    'image/png'
  when '.gif'
    'image/gif'
  when '.bmp'
    'image/bmp'
  when '.webp'
    'image/webp'
  else
    'application/octet-stream'
  end
end