3. File Storage
Upload Files
Hat for iOS allows you to also upload files to your PDA. You can read more about PDA File Storage here.
The uploading of a file is a multi-step process:
Request from
PDAa temporaryURLto upload the fileUpload the file to the
URLMark the file as complete
Step 1
To start a file upload you have to call the function below:
HATFileService.uploadFileToHAT(
fileName: name,
token: token,
userDomain: userDomain,
tags: tags,
completion: completionCallback,
errorCallback: failCallback
)fileNameis the desired name of the file. You can either randomise it or provide meaningful names, it's up to you.tokenis the user's token. This is needed to authenticate the user with thePDA.userDomainis the user'sPDA Address. This is needed to form theURLto upload the file.tagsis an array ofStringwith useful tags to make searching for that file easier.completionis an optional callback function of type((FileUploadObject, String?) -> Void)?,FileUploadObjectis a custom object ofHat for iOSdescribing the file upload object. The OptionalStringis the refreshed token returned from thePDA. This is executed on success and it includes theURLthat you have to upload your file.errorCallbackis an optional callback that is executed when the request has failed. The type of the callback function is((HATTableError) -> Void)?.HATTableErroris a custom object describing the errors that have occurred during the querying of the tables in the database.
For the purpose of this example, let's say that fileName is testfile, tags is just test-tag, userDomain is postman.hubat.net and token is token. That will translate to the request below:
HATFileService.uploadFileToHAT(
fileName: "testfile",
token: "token",
userDomain: "postman.hubat.net",
tags: ["test-tag"],
completion: completionCallback,
errorCallback: failCallback
)A successful request will result in a status code 200 and the response will look like the one below:
{
"fileId": "dataswift-sandboxtestfile",
"name": "testfile",
"source": "dataswift-sandbox",
"dateCreated": "2018-11-08T16:08:38.679Z",
"lastUpdated": "2018-11-08T16:08:38.679Z",
"tags": [
"test-tag"
],
"status": {
"status": "New"
},
"contentUrl": "https://hubat-net-hatservice-v3ztbxc9civz-storages3bucket-m0gs7co0oyi2.s3.eu-west-1.amazonaws.com/postman.hubat.net/dataswift-sandboxtestfile?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20181108T160838Z&X-Amz-SignedHeaders=host%3Bx-amz-server-side-encryption&X-Amz-Expires=300&X-Amz-Credential=AKIAICFRCZUZIP4PQ64A%2F20181108%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=f868940fad6ad21085c19b62d4b940127808858e64e57dd44b4de452178e107d",
"contentPublic": false,
"permissions": [
{
"userId": "de35e18d-14wf-4664-8de7-409abf881754",
"contentReadable": true
}
]
}The above response is described by FileUploadObject. The values are explained below:
fileIdis the file's ID on thePDA, it is formed by combining thesourceandnameof the filenameis the name of the file that we assigned. In case of a duplicatefileId,PDAwill append an incremented number at the endsourceis the source that requested to upload this file. It's good to have your app name as thesource.dateCreatedis the date that the file was created inISOformat. OptionallastUpdatedis the date that the file was last modified. You can change thename,statusand make the file private or not. Optionaltagsis an array ofStringthat allows for easier search of the file in the future. You can add multiple tags.statusis a[String: String]Key-Value pair and it can beNew, when you just requested the file upload, orCompletedafter the uploading has finished and you marked the file asCompleted.contentUrlis theURLyou will use to upload the file. ThisURLis short-lived as its use is just to provide aURLfor your app to upload the file.contentPublicis the flag that indicates if the file isprivateorpublic. You can mark a file asprivateorpublicafter the file has been uploaded.permissionsis an array of Key-Value pairs of type[String: Any]. In the above example, the permissions provide the user with thatuserIdthe ability to have access to the file even if the file is marked asprivate.
A request that has failed will look like this:
{
"error": "Not Authenticated",
"message": "Not Authenticated"
}erroris the error that has occurredmessageis a more descriptive message about theerrorthat has occurred
Step 2
The second step is to upload the actual file. There is no limitation on what type of file you can upload. You can upload images, videos, documents etc. It's up to the application to ensure that the file is uploaded correctly and that it can download the file and read it without problems.
To upload a file you can use the function below included in Hat for iOS:
HATNetworkHelper.uploadFile(
image: data,
url: contentURL,
progressUpdateHandler: progressUpdater,
completion: completionCallback
)imageis type ofData. This function requires that you convert your file intoDatatype first. You can convert aJPEGimage toDatawithimage.jpegData(compressionQuality: 1.0)urlis theURLto upload the file. As we said earlier, thisURLis thecontentUrlprovided by thePDAin the first step.progressUpdateHandleris an optional function callback of type((Double) -> Void)?. It continuously reports the status of the upload from 0 to 1. For example 0.10 is 10%.completionis a callback function of type@escaping (_ r: HATNetworkHelper.ResultType) -> Void). It can be eitherisSuccess(isSuccess: Bool, statusCode: Int?, result: JSON, token: String?)when the request was successful orerror(error: Error, statusCode: Int?, result: JSON?)when the request failed for some reason. You can read more about it here.
A successful request will look almost exactly like the original one:
{
"fileId": "dataswift-sandboxtestfile",
"name": "testfile",
"source": "dataswift-sandbox",
"dateCreated": "2018-11-08T16:08:38.679Z",
"lastUpdated": "2018-11-08T16:08:40.235Z",
"tags": [
"test-tag"
],
"status": {
"status": "New"
},
"contentUrl": "https://hubat-net-hatservice-v3ztbxc9civz-storages3bucket-m0gs7co0oyi2.s3.eu-west-1.amazonaws.com/postman.hubat.net/dataswift-sandboxtestfile?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20181108T160838Z&X-Amz-SignedHeaders=host%3Bx-amz-server-side-encryption&X-Amz-Expires=300&X-Amz-Credential=AKIAICFRCZUZIP4PQ64A%2F20181108%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=f868940fad6ad21085c19b62d4b940127808858e64e57dd44b4de452178e107d",
"contentPublic": false,
"permissions": [
{
"userId": "de35e18d-14wf-4664-8de7-409abf881754",
"contentReadable": true
}
]
}The only thing that has now changed is the lastUpdated field.
A request that has failed will look like this:
{
"error": "Not Authenticated",
"message": "Not Authenticated"
}Step 3
When the file has finished uploading it's required to mark the file as completed, or else the file will be deleted. To do so you have to make another request. To achieve that you have to call the next function:
HATFileService.completeUploadFileToHAT(
fileID: fileObject.fileID,
token: token,
tags: tags,
userDomain: userDomain,
completion: completionCallback,
errorCallback: failCallback
)fileIDis the file id that is to be marked ascompletedtokenis the user's token. This is needed to authenticate the user with thePDA.tagsis an array ofStringwith useful tags to make searching for that file easier. This is added beforeHat for iOSreturns you the full file.userDomainis the user'sPDA Address. This is needed to form theURLto mark the file ascompleted.completionis an optional callback function of type((FileUploadObject, String?) -> Void)?,FileUploadObjectis a custom object ofHat for iOSdescribing the file upload object. The OptionalStringis the refreshed token returned from thePDA.errorCallbackis an optional callback that is executed when the request has failed. The type of the callback function is((HATTableError) -> Void)?.HATTableErroris a custom object describing the errors that have occurred during the querying of the tables in the database.
A successful request will look almost exactly like the original one:
{
"fileId": "dataswift-sandboxtestfile",
"name": "testfile",
"source": "dataswift-sandbox",
"dateCreated": "2018-11-08T16:08:38.679Z",
"lastUpdated": "2018-11-08T16:08:43.157Z",
"tags": [
"test-tag"
],
"status": {
"size": 123,
"status": "Completed"
},
"contentUrl": "https://hubat-net-hatservice-v3ztbxc9civz-storages3bucket-m0gs7co0oyi2.s3.eu-west-1.amazonaws.com/postman.hubat.net/dataswift-sandboxtestfile?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20181108T160838Z&X-Amz-SignedHeaders=host%3Bx-amz-server-side-encryption&X-Amz-Expires=300&X-Amz-Credential=AKIAICFRCZUZIP4PQ64A%2F20181108%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=f868940fad6ad21085c19b62d4b940127808858e64e57dd44b4de452178e107d",
"contentPublic": false,
"permissions": [
{
"userId": "de35e18d-14wf-4664-8de7-409abf881754",
"contentReadable": true
}
]
}The only thing that has changed is the lastUpdated field and the status field. The status is marked as Completed and includes the file size in bytes.
A request that has failed will look like this:
{
"error": "Not Authenticated",
"message": "Not Authenticated"
}erroris the error that has occurredmessageis a more descriptive message about theerrorthat has occurred
Mark file as public
By default the uploaded files are marked as private. You can change this by calling this function:
HATFileService.makeFilePublic(
fileID: file.fileID,
token: userToken,
userDomain: userDomain,
successCallback: completionCallback,
errorCallBack: errorCallBack)fileIDis the file id that is to be marked aspublictokenis the user's token. This is needed to authenticate the user with thePDA.userDomainis the user'sPDA Address. This is needed to form theURLto mark the file aspublic.completionis an optional callback function of type(Bool -> Void)returnstrueerrorCallbackis an optional callback that is executed when the request has failed. The type of the callback function is((HATError) -> Void)?.HATErroris a custom object describing the general error that occurred on thePDA.
A successful request will have a statusCode of 200 and look almost exactly like the original one:
{
"fileId": "dataswift-sandboxtestfile",
"name": "testfile",
"source": "dataswift-sandbox",
"dateCreated": "2018-11-08T16:08:38.679Z",
"lastUpdated": "2018-11-08T16:04:45.689Z",
"tags": [
"test-tag"
],
"status": {
"size": 123,
"status": "Completed"
},
"contentUrl": "https://hubat-net-hatservice-v3ztbxc9civz-storages3bucket-m0gs7co0oyi2.s3.eu-west-1.amazonaws.com/postman.hubat.net/dataswift-sandboxtestfile?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20181108T160838Z&X-Amz-SignedHeaders=host%3Bx-amz-server-side-encryption&X-Amz-Expires=300&X-Amz-Credential=AKIAICFRCZUZIP4PQ64A%2F20181108%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=f868940fad6ad21085c19b62d4b940127808858e64e57dd44b4de452178e107d",
"contentPublic": true,
"permissions": [
{
"userId": "de35e18d-14wf-4664-8de7-409abf881754",
"contentReadable": true
}
]
}The only thing that has now changed is the lastUpdated field and the contentPublic field.
A request that has failed will look like this:
{
"error": "Not Authenticated",
"message": "Not Authenticated"
}erroris the error that has occurredmessageis a more descriptive message about theerrorthat has occurred
Mark file as Private
Be default the uploaded files are marked as private. You can change that by calling this function:
HATFileService.makeFilePrivate(
fileID: file.fileID,
token: userToken,
userDomain: userDomain,
successCallback: completionCallback,
errorCallBack: errorCallBack)fileIDis the file id that is to be marked asprivatetokenis the user's token. This is needed to authenticate the user with thePDA.userDomainis the user'sPDA Address. This is needed to form theURLto mark the file asprivate.completionis an optional callback function of type(Bool -> Void)returnstrueerrorCallbackis an optional callback that is executed when the request has failed. The type of the callback function is((HATError) -> Void)?.HATErroris a custom object describing the general error that occurred on thePDA.
A successful request will have a statusCode of 200 and look almost exactly like the original one:
{
"fileId": "dataswift-sandboxtestfile",
"name": "testfile",
"source": "dataswift-sandbox",
"dateCreated": "2018-11-08T16:08:38.679Z",
"lastUpdated": "2018-11-08T16:09:44.247Z",
"tags": [
"test-tag"
],
"status": {
"size": 123,
"status": "Completed"
},
"contentUrl": "https://hubat-net-hatservice-v3ztbxc9civz-storages3bucket-m0gs7co0oyi2.s3.eu-west-1.amazonaws.com/postman.hubat.net/dataswift-sandboxtestfile?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20181108T160838Z&X-Amz-SignedHeaders=host%3Bx-amz-server-side-encryption&X-Amz-Expires=300&X-Amz-Credential=AKIAICFRCZUZIP4PQ64A%2F20181108%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=f868940fad6ad21085c19b62d4b940127808858e64e57dd44b4de452178e107d",
"contentPublic": false,
"permissions": [
{
"userId": "de35e18d-14wf-4664-8de7-409abf881754",
"contentReadable": true
}
]
}The only thing that has now changed is the lastUpdated field and the contentPublic field.
A request that has failed will look like this:
{
"error": "Not Authenticated",
"message": "Not Authenticated"
}erroris the error that has occurred.messageis a more descriptive message about theerrorthat has occurred
Search files
You can also search for files with Hat for iOS. To do so you can use the next function:
HATFileService.searchFiles(
userDomain: userDomain,
token: userToken,
status: "Completed",
name: "",
tags: tags,
successCallback: receivedImages,
errorCallBack: receivedErrorGettingImages)userDomainis the user'sPDA Address. This is needed to form theURLto fetch the files.tokenis the user's token. This is needed to authenticate the user with thePDA.statusis the status of the file. You can search forCompletedfiles only or forNewfiles.nameis the name of the file. You can search for a specific file or leave this as an emptyStringin case you want to fetch all the files that match thestatusandtags.tagsis an array ofStringwith useful tags to make searching for that file easier. You can search file with specific tags.completionis a callback function of type@escaping ([FileUploadObject], String?) -> Void. The first parameter is an array ofFileUploadObject, it contains all the files that theDAfound during the search. The second parameter is an optionalString, it is a refreshed token returned from thePDA. It can benil.errorCallbackis an optional callback that is executed when the request has failed. The type of the callback function is((HATError) -> Void)?.HATErroris a custom object describing the general error that occurred on thePDA.
A successful request will have a statusCode of 200 and look like this:
[
{
"fileId": "rumpel1532526508004.jpg",
"name": "1532526508004.jpg",
"source": "rumpel",
"dateCreated": "2018-07-25T13:48:24.976Z",
"lastUpdated": "2018-07-25T13:48:24.976Z",
"tags": [
"android",
"image/jpeg"
],
"status": {
"size": 144332,
"status": "Completed"
},
"contentUrl": "https://hubat-net-hatservice-v3zbxc9civz-storages3bucket-m0gs7co0oyi2.s3.eu-west-1.amazonaws.com/postman.hubat.net/rumpel1532526508004.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20181109T184507Z&X-Amz-SignedHeaders=host&X-Amz-Expires=299&X-Amz-Credential=AKIAICFRCZUZIP4PQ64A%2F20181109%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=3492955b3870ab6bd321f43ba18cb62cd538547b7e523a57c87e0355c29eb3c3",
"contentPublic": true,
"permissions": [
{
"userId": "de35e1ed-147f-4664-8de7-409abf881754",
"contentReadable": true
}
]
},
{
"fileId": "rumpelrumpelphoto-49",
"name": "rumpelPhoto",
"source": "rumpel",
"dateCreated": "2018-06-18T08:31:10.420Z",
"lastUpdated": "2018-06-18T08:31:10.420Z",
"tags": [
"iphone",
"notes",
"photo"
],
"status": {
"size": 2066062,
"status": "Completed"
},
"contentUrl": "https://hubat-net-hatservice-v3ztbxc9ciz-storages3bucket-m0gs7co0oyi2.s3.eu-west-1.amazonaws.com/postman.hubat.net/rumpelrumpelphoto-49?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20181109T184507Z&X-Amz-SignedHeaders=host&X-Amz-Expires=299&X-Amz-Credential=AKIAICFRCZUZIP4PQ64A%2F20181109%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=2cd122df4cdf15be3e66889d5cc2e525880eb4f2c82012b6dcf00c601ad390da",
"contentPublic": true,
"permissions": [
{
"userId": "de35e1ed-147f-4664-8de7-409abf881754",
"contentReadable": true
}
]
}
]A request that has failed will look like this:
{
"error": "Not Authenticated",
"message": "Not Authenticated"
}erroris the error that has occurredmessageis a more descriptive message about theerrorthat has occurred
Delete files
You can also delete files from the PDA using the next function:
HATFileService.deleteFile(
fileID: fileID,
token: userToken,
userDomain: userDomain,
successCallback: fileDeleted,
errorCallBack: fileFailedToDelete)fileIDis the file id on thePDAtokenis the user's token. This is needed to authenticate the user with thePDA.userDomainis the user'sPDA Address. This is needed to form theURLto delete the file.completionis a callback function of type@escaping (Bool, String?) -> Void. The first parameter is true. The second parameter is an optionalString, it is a refreshed token returned from thePDA. It can benil.errorCallbackis an optional callback that is executed when the request has failed. The type of the callback function is((HATError) -> Void)?.HATErroris a custom object describing the general error that occurred on thePDA.
A successful request will have a statusCode of 200 and look almost exactly like the original one:
{
"fileId": "dataswift-sandboxtestfile",
"name": "testfile",
"source": "dataswift-sandbox",
"dateCreated": "2018-11-08T16:08:38.679Z",
"lastUpdated": "2018-11-08T16:09:44.247Z",
"tags": [
"test-tag"
],
"status": {
"status": "Deleted"
},
"contentUrl": "https://hubat-net-hatservice-v3ztbxc9civz-storages3bucket-m0gs7co0oyi2.s3.eu-west-1.amazonaws.com/postman.hubat.net/dataswift-sandboxtestfile?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20181108T160838Z&X-Amz-SignedHeaders=host%3Bx-amz-server-side-encryption&X-Amz-Expires=300&X-Amz-Credential=AKIAICFRCZUZIP4PQ64A%2F20181108%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=f868940fad6ad21085c19b62d4b940127808858e64e57dd44b4de452178e107d",
"contentPublic": false,
"permissions": [
{
"userId": "de35e18d-14wf-4664-8de7-409abf881754",
"contentReadable": true
}
]
}The only thing that has now changed is the status: to Deleted.
A request that has failed will look like this:
{
"error": "Not Authenticated",
"message": "Not Authenticated"
}erroris the error that has occurredmessageis a more descriptive message about theerrorthat has occurred
Last updated
Was this helpful?