Skip to content

Latest commit

 

History

History
96 lines (65 loc) · 2.47 KB

StorageClient.md

File metadata and controls

96 lines (65 loc) · 2.47 KB

StorageClient

StorageClient is a client that helps me use a firebase-storage interface with ease.

About StorageReference

In StorageClient, StorageReference is either Resource or Folder.

Resource

Resource is a content that can store in FirebaseStorage, such as Text, Image, Video, Audio and so on.

Although Resource is content, Resouce is stored in Folder as one of a resouce in single folder.

"My Photo" is Resource
スクリーンショット 2022-03-13 0 27 45

Folder

All of Resources are in Folder. There are three kinds of Folder.

HomeFolder

HomeFolder is at the real root of folder.

RootFolder

RootFolder is a child of HomeFolder. Although HomeFolder is ensured to be single, RootFolder can be multiple. So, this is root Folder created by developers.

SubFolder

SubFolder is a child of RootFolder.

"StorageExample" is RootFolder
スクリーンショット 2022-03-13 0 43 10

Usage

  1. Init StorageClient.

First of all, configure StorageClient.shared.storage with own FirebaseStorage.Storage.

// Option: If you want to select specific Storage, add this line.
StorageClient.shared.storage = Storage.storage()

Upload

  1. Create new Resource and upload.

It is possible to upload new resource like the following.

Because all Resource are under Folder, it is also necessary to set parent folder at Resource.folder.

func upload_photo() {
    let folder = RootFolder(name: "Tests")
    let text: String = "Test Test Test Test Text!!"
    let data = text.data(using: .utf8)!
    let resource = Resource(
        name: "test",
        folder: folder,
        metadata: Resource.Metadata(contentType: .plain),
        data: data
    )
    // Start uploading
    resource.uploadWithData()
        .sink { task in
            switch task.status {
            case .success:
                print("OK")
            case .fail(let error):
                print(error)
            case .progress(let fractionComplete):
                print(fractionComplete)
            default:
                break
            }
        }
        .store(in: &cancellables)
}

Resource.Task

Documentation is work in progress.