Class: GFClient

Inherits:
Object
  • Object
show all
Defined in:
lib/gofile_ruby.rb

Overview

A wrapper for the GoFile API, containing methods for each available endpoint.

Instance Method Summary collapse

Constructor Details

#initialize(token: nil, guest: false) ⇒ GFClient

Creates a new instance of the GFClient class.

Parameters:

  • token (String) (defaults to: nil)

    The API token for your GoFile account

  • guest (Boolean) (defaults to: false)

    A boolean value indicating whether or not guest mode should be enabled



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

def initialize(token:nil, guest:false)
  @token = token
  # TODO: Change the name of this parameter! @isGuest is used to check if the user has an account token, not to be confused with *guest accounts*, which have API tokens.
  # A GFClient in guest mode (where no token is provided) will set @isGuest to false after acquiring the API key from a newly created guest account, after the user uploads their first file in guest mode.
  @isGuest = guest
  @accDetails = nil
  # Gets set after a guest account uploads their first file
  # Is used only when uploading files as a guest
  @guestUploadDestination = nil

  # Automatically set guest mode if user doesn't provide any input
  @isGuest = true if !@token && !@isGuest
  # If user tries inputting a token while also enabling guest mode, switch guest mode off
  @isGuest = false if @token
   unless @isGuest
end

Instance Method Details

#copy_content(destination_id:, contents_id:) ⇒ Hash

TODO:

This method will be tested at a later time due to it being a premium-only endpoint.

Note:

This method is premium only! You will not be able to use it unless you have a premium account!

Copies one or multiple contents to destination folder.

Destination ID: String

Contents ID: String A string of comma separated content ID’s. (“id1,id2,id3”) Response example:

"status": "ok",
"data": {}

Parameters:

  • destination_id (String)

    The ID for the folder where the contents will be copied to.

  • contents_id: (String)

    A string of comma separated content ID’s. (“id1,id2,id3”)

Returns:

  • (Hash)

    response The response object.



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/gofile_ruby.rb', line 220

def copy_content(destination_id:, contents_id:)
  copy_url = "https://api.gofile.io/copyContent"

  body = {
    "contentsId": contents_id,
    "folderIdDest": destination_id,
    "token": @token
  }

  ret = HTTPHelper.put(copy_url, body)

  ret
end

#create_folder(parent_id: nil, folder_name:) ⇒ Hash

Creates a folder with the given folder name and parent ID. If a parent ID is not provided, it default to the root folder.

When using guest mode, you cannot call this method until you’ve uploaded a file first, as the guest token and root folder ID won’t be available.

Example response:

"status": "ok",
"data": {

}

Parameters:

  • parent_id (String) (defaults to: nil)

    The ID of the parent folder

  • folder_name (String)

    The name of the folder that will be created

Returns:

  • (Hash)

    response The response object.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gofile_ruby.rb', line 104

def create_folder(parent_id:nil, folder_name:)
  raise "Cannot create folders in guest mode! Did you mean to upload a file instead?" if @isGuest
  post_folder_url = "https://api.gofile.io/createFolder"
  
  parent_id = @accDetails["data"]["rootFolder"] unless parent_id
  folder_data = {
    "parentFolderId" => parent_id,
    "folderName" => folder_name,
    "token" => @token
  }

  ret = HTTPHelper.put(post_folder_url, folder_data)
  
  ret
end

#delete_content(contents_id:) ⇒ Hash

Delete one or multiple contents.

Response example:

"status": "ok",
"data": {}

Parameters:

  • contents_id (String)

    A string of comma separated content ID’s. (“id1,id2,id3”)

Returns:

  • (Hash)

    response The response object.



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/gofile_ruby.rb', line 241

def delete_content(contents_id:)
  delete_url = "https://api.gofile.io/deleteContent"

  body = {
    "contentsId": contents_id,
    "token": @token
  }

  ret = HTTPHelper.delete(delete_url, body)

  ret
end

#get_account_detailsHash

Will return the details of the current account.

Response example:

"status": "ok",
"data": {
  "token": "ivlW1ZSGn2Y4AoADbCHUjllj2cO9m3WM",
  "email": "email@domain.tld",
  "tier": "standard",
  "rootFolder": "2aecea58-84e6-420d-b2b9-68b4add8418d",
  "foldersCount": 4,
  "filesCount": 20,
  "totalSize": 67653500,
  "totalDownloadCount": 1
}

Returns:

  • (Hash)

    response The response object.



269
270
271
272
273
274
275
# File 'lib/gofile_ruby.rb', line 269

def 
   = "https://api.gofile.io/getAccountDetails?token=#{@token}"
  details = HTTPHelper.get()   
  @accDetails = details

  details
end

#get_children(parent_id: nil) ⇒ Hash

TODO:

This method will be tested at a later time due to it being a premium-only endpoint.

Note:

This method is premium only! You will not be able to use it unless you have a premium account!

Gets the children of a specific folder.

Defaults to root folder if a parent ID is not provided.

Response example:

"status": "ok",
"data": {
  "isOwner": true,
  "id": "3dbc2f87-4c1e-4a81-badc-af004e61a5b4",
  "type": "folder",
  "name": "Z19n9a",
  "parentFolder": "3241d27a-f7e1-4158-bc75-73d057eff5fa",
  "code": "Z19n9a",
  "createTime": 1648229689,
  "public": true,
  "childs": [
    "4991e6d7-5217-46ae-af3d-c9174adae924"
  ],
  "totalDownloadCount": 0,
  "totalSize": 9840497,
  "contents": {
    "4991e6d7-5217-46ae-af3d-c9174adae924": {
      "id": "4991e6d7-5217-46ae-af3d-c9174adae924",
      "type": "file",
      "name": "example.mp4",
      "parentFolder": "3dbc2f87-4c1e-4a81-badc-af004e61a5b4",
      "createTime": 1648229689,
      "size": 9840497,
      "downloadCount": 0,
      "md5": "10c918b1d01aea85864ee65d9e0c2305",
      "mimetype": "video/mp4",
      "serverChoosen": "store4",
      "directLink": "https://store4.gofile.io/download/direct/4991e6d7-5217-46ae-af3d-c9174adae924/example.mp4",
      "link": "https://store4.gofile.io/download/4991e6d7-5217-46ae-af3d-c9174adae924/example.mp4"
    }
  }
}

Parameters:

  • parent_id (String) (defaults to: nil)

    The ID of the parent folder.

Returns:

  • (Hash)

    response The response object.



161
162
163
164
165
166
167
168
169
# File 'lib/gofile_ruby.rb', line 161

def get_children(parent_id:nil)
  raise "Guests cannot use the #get_children method!" if @isGuest
  parent = @accDetails["data"]["rootFolder"] if !parent

  content_url = "https://api.gofile.io/getContent?contentId=#{parent}&token=#{@token}"
  ret = HTTPHelper.get(content_url)

  ret
end

#get_serverHash

Retreives the best available server for uploading files

Example response:

"status": "ok",
"data": {
  "server": "store1"
}

Returns:

  • (Hash)

    response The response object.



38
39
40
41
# File 'lib/gofile_ruby.rb', line 38

def get_server
  server_url = "https://api.gofile.io/getServer"
  HTTPHelper.get(server_url)
end

#set_folder_option(folder_id:, option:, value:) ⇒ Hash

Sets the options for a specific folder.

The expected option and value types are listed below.

public: Boolean

password: String

description: String

expire: Unix Timestamp

tags: String (String of comma separated tags, Eg. “tag1,tag2,tag3”)

Response example:

"status": "ok",
"data": {}

Parameters:

  • folder_id (String)

    The ID of the target folder.

  • option (String)

    The option that you wish to set. Can be “public”, “password”, “description”, “expire” or “tags”

  • value (String)

    The matching value for the option parameter

Returns:

  • (Hash)

    response The response object.



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/gofile_ruby.rb', line 192

def set_folder_option(folder_id:, option:, value:)
  options_url = "https://api.gofile.io/setFolderOption"

  body = {
    "option": option,
    "value": value,
    "folderId": folder_id,
    "token": @token
  }
  ret = HTTPHelper.put(options_url, body)

  ret
end

#upload_file(file:, folder_id: nil) ⇒ Hash

Uploads a file to the given destination folder.

If using guest mode, you cannot specify a folder ID when uploading your first file.

If you’re uploading multiple files, you have to use the parent ID along with the token from the response object in your subsequent uploads.

To get around this issue, gofile_ruby saves the newly returned token and parent ID after your first upload.

This means that you can call the #upload_file method multiple times without having to deal with authentication.

Example response:

"status": "ok",
 "data": {
   "guestToken": "a939kv5b43c03192imatoken2949"
   "downloadPage": "https://gofile.io/d/Z19n9a",
   "code": "Z19n9a",
   "parentFolder": "3dbc2f87-4c1e-4a81-badc-af004e61a5b4",
   "fileId": "4991e6d7-5217-46ae-af3d-c9174adae924",
   "fileName": "example.mp4",
   "md5": "10c918b1d01aea85864ee65d9e0c2305"
 }

Parameters:

  • file (File)

    The file that will be uploaded to GoFile

  • folder_id (String) (defaults to: nil)

    The ID for the parent folder. Will default to the root folder if none is provided.

Returns:

  • (Hash)

    response The response object.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gofile_ruby.rb', line 67

def upload_file(file:, folder_id: nil)
  raise "Guests cannot specify folder ID before their first upload!" if folder_id && @guestUploadDestination.nil?

  best_server = get_server()["data"]["server"]
  upload_url = "https://#{best_server}.gofile.io/uploadFile"

  body = [["file", file]]
  body << ["token", @token] if !@isGuest

  # If user inputs a folder_id while they aren't in guest mode,
  if folder_id && !@isGuest
    # add the ID to the body of the request
    body << ["folderId", folder_id]
  # If the user is on a guest account, and a folder id hasn't been provided, use the folder ID returned from the first file upload
  elsif @guestUploadDestination && !folder_id
    body << ["folderId", @guestUploadDestination]
  end

  ret = HTTPHelper.post_multipart_data(upload_url, body)
  save_guest_acc_details(ret) if @isGuest

  ret
end