NimarkoGram Docs
RU
Docs/Custom file actions

Custom file actions

Register an extension and handle a document being opened.

On this pagePrepare a documentRegister the extensionDecide who handles the openCheck the boundaries

Prepare a document

Save the text abc as UTF-8 in sample.ngsample with no trailing newline. The result is three bytes. Send it to Saved Messages as a document. Install and enable reader-en.plugin, download the document and tap it. Expect a bulletin reading “sample.ngsample · 3 bytes”. An editor that adds a newline or byte-order mark will produce a larger file.

Register the extension

FilesController.register takes the active plugin ID, an extension and a callback. The engine converts extensions to lowercase and adds a leading dot when needed. Register in on_plugin_load. The return value is a registration handle or None. If another plugin owns the extension, registration can raise FilesController.ExtensionAlreadyRegistered.

Python
__id__ = "sdk_reader"
__name__ = "File details"
__author__ = "NimarkoGram"
__version__ = "1.0"
__host__ = "NimarkoGram"
__sdk_version__ = ">=1.0"

from base_plugin import BasePlugin
from file_utils import FilesController
from ui.bulletin import BulletinHelper


class FileDetails(BasePlugin):
    def on_plugin_load(self):
        self.handle = FilesController.register(
            self.id, ".ngsample", self.show_details
        )
        if self.handle is None:
            self.log("Could not register .ngsample")

    def show_details(self, info):
        BulletinHelper.show_info(f"{info.name} · {info.size} bytes")
        return True

Decide who handles the open

The callback receives a FileInfo. Use name for the display name, size for the byte count and path for the local path. Showing details requires no file read. The example returns the boolean True after showing its bulletin, marking the open as handled. Return False to allow normal handling. Falling off the end of the function also allows normal handling.

Check the boundaries

Try the same file with a .txt extension. The plugin should leave it alone. Disable the plugin and open the .ngsample document again. Its bulletin should disappear. To drop a registration while the plugin is active, call unregister() on the saved handle. For reading larger files, see Background and UI queues.