Java/Groovy API

File Field is compatible with all Jira automation apps: ScriptRunner, Automation for Jira, JSU, etc. You can use the FileManager java component to manage your files programmatically from java/groovy scripts.

Plain Java Example

import com.apwide.file.api.FileManager;
...
import com.atlassian.jira.component.*;
// Get FileManager instance
FileManager fileManager = ComponentAccessor.getComponent(FileManager.class);
// Binary content to upload
InputStream inputStream = new ByteArrayInputStream("any content".getBytes(StandardCharsets.UTF_8));
// Name of the file to upload
String fileName = "Readme.txt";
// Size limit of file to upload (ex: 10Mo)
long maxFileSize = 10*1000*1000;
// UPLOAD
String fileId = fileManager.upload(fileName, inputStream, maxFileSize);
// DONWLOAD
InputStream downloadedFile = fileManager.download(fileId);
// GET PATH
String filePath = fileManager.getPath(fileId);
// CHECK IF IT EXISTS
boolean exists = fileManager.exists(fileId);
// DELETE
boolean deleted = fileManager.delete(fileId);234567891011121314151617181920212223242526272829

Script Runner Groovy Example

This example script shows how to create/upload 2 files and add them to an existing File Field customfield.

N.B. Adding multiple uploaded files into 1 single customfield is supported since version 3.1.0.

You should always set the value of a File customfield using a unique String:

  • containing 1 file id. Example: "1568037617780_10000_w3c_home.gif"

  • or containing multiple file ids separated by a comma. Example: "1568037617780_10000_w3c_home.gif,1568037613455_10000_another_file.gif"

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule
@WithPlugin("com.apwide.document.file-field")
import com.apwide.file.api.FileManager;
import com.atlassian.jira.component.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@PluginModule
FileManager fileManager
// create and upload first file
String textFileContent = "My first example file"
InputStream inputStream = new ByteArrayInputStream(textFileContent.getBytes(StandardCharsets.UTF_8))
String fileId_1 = fileManager.upload("My First example file.txt", inputStream)
// create and upload second file
String textFileContent = "My second example file"
InputStream inputStream = new ByteArrayInputStream(textFileContent.getBytes(StandardCharsets.UTF_8))
String fileId_2 = fileManager.upload("My Second example file.txt", inputStream)
// Update the customfield
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("HSP-31")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fileField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "file"}
// get current value of the file field for an Issue if you need it (file ids. String with following format: "fileId1,fileId2")
def currentFileIds = fileField.getValue(issue)
if (fileField) {
def changeHolder = new DefaultIssueChangeHolder()
// update customfield's value using a String containing file ids separated by a comma
fileField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fileField), fileId_1 + "," + fileId_2), changeHolder)
}

JavaDoc

String upload(String fileName, InputStream file, long maxFileSize)

Upload file

  • Parameters:

    • fileName — name of the file

    • file — file content

    • maxFileSize — max file size allowed (in bytes)

  • Returns: generated unique id of the uploaded file

boolean exists(String fileId)

Check if file exists

  • Parameters: fileId — unique id of the file

  • Returns: true if file exists, false otherwise

InputStream download(String fileId) throws FileNotFoundException

Donwload file

  • Parameters: fileId — unique id of the file

  • Returns: content of the file

  • Exceptions: FileNotFoundException — if file could not be found

boolean delete(String fileId) throws FileNotFoundException

Delete file

  • Parameters: fileId — unique id of the file

  • Returns: true if file was deleted

  • Exceptions: FileNotFoundException — if file could not be found

String getPath(String fileId) throws FileNotFoundException

Get file path from file id

  • Parameters: fileId — unique id of the file

  • Returns: file path of the file

  • Exceptions: FileNotFoundException — if file could not be found

Java API Interface

package com.apwide.file.api;
import java.io.FileNotFoundException;
import java.io.InputStream;
public interface FileManager {
/**
* Upload file
* @param fileName name of the file
* @param file file content
* @param maxFileSize max file size allowed (in bytes)
* @return generated unique id of the uploaded file
*/
String upload(String fileName, InputStream file, long maxFileSize);
/**
* Check if file exists
* @param fileId unique id of the file
* @return true if file exists, false otherwise
*/
boolean exists(String fileId);
/**
* Donwload file
* @param fileId unique id of the file
* @return content of the file
* @throws FileNotFoundException if file could not be found
*/
InputStream download(String fileId) throws FileNotFoundException;
/**
* Delete file
* @param fileId unique id of the file
* @return true if file was deleted
* @throws FileNotFoundException if file could not be found
*/
boolean delete(String fileId) throws FileNotFoundException;
/**
* Get file path from file id
* @param fileId unique id of the file
* @return file path of the file
* @throws FileNotFoundException if file could not be found
*/
String getPath(String fileId) throws FileNotFoundException;
}