Syncing & reacting to file changes
Sync
This docs page is for you if you
Are trying to keep the state of your drive file tree in sync with something else
"Something else" being a database, a spreadsheet, a local file system, or a remote file system
Want to make some automation off of a created/deleted/moved file or folder
This page is NOT for you if
You're not a developer — we'll be making an easier-to-use version of this soon
Theory
The most reliable way to sync two systems is to give references to the changes over time, allowing the client trying to perform the sync to acknowledge, replay, apply the diff before ensuring database integrity and moving onto the next change
An example of this is like in git VCS where there are a series of commits and the difference between two commits is given by a "patch" if you want to end up with the current state of the tree you apply these patches to your local system which will add together to end up with the resultant state (ok not exactly like that, but it works in this example).
The sync is similar though in this case where you query our servers for a patch, if there's a new patch you apply that patch to your system — at the end of the day when you sum everything it will be correct.
How to
Getting the patch diff
Get an API key
Set up your listener system to periodically use that key to redeem a ShadeFS JWT using the route
GET https://api.shade.inc/workspaces/drives/{drive_id}/shade-fs-tokenThe account the API key belongs to needs edit access to the drive
This will return a JWT that is currently valid for one hour — read its
expclaim and fetch a new one before it expires
Now you can use that JWT to see the current "head" for your drive, this is a hash that represents the current state
Do this by calling
GET https://fs.shade.inc/{drive_id}/heads/current, with your JWT as the "Authorization" header — this will return a hash like972d06bb83b2979f7bd5d2e1e98aa0da2b763ffa7a5a408fee27c1d561e45754
To get your first patch diff you can get the lifetime history of the drive
Do this by calling
GET https://fs.shade.inc/{drive_id}/heads/compare?to=your_hash_you_gotThis will give back a JSON object with
drive,startCommit,endCommit, and apatchDiffobject containing the changes (see below)
As heads come in, the result returned by
heads/current, compare that against the previous patch head you applied, if they're different, then perform the patch diff withGET https://fs.shade.inc/{drive_id}/heads/compare?from=previous_head&to=new_head
What's in the patch diff
All changes are under the patchDiff key. Every path is an array of path segments relative to the drive root, for example ["Project", "Ep 1", "clip.mov"].
deletedFolders
Folders that were deleted
deletedFiles
All deleted files, including null, draft, and main files
renames
Moves and renames. Each entry has a before path and an after path
createdMainFiles
New files that are fully uploaded
createdDraftFiles
New files that are still uploading
createdNullFiles
New empty files
nullOrDraftToMainFiles
Existing null or draft files that are now fully uploaded
mainOrNullToDraftFiles
Existing main or null files that are being uploaded again
draftOrMainToNullFiles
Existing draft or main files that became empty
modifiedMainFiles
Main files whose contents changed
modifiedDraftFiles
Draft files whose contents changed
createdFolders
Folders that were created
Applying the patch diff
Now that you have a diff it can be parsed into changes fairly easily An entry for the "blob state" must be kept as well if its "null" "draft" or "main" Null means an empty file, draft means a file thats still being uploaded and is inaccessible and main means a fully uploaded and accessible file
First delete the folders in
deletedFolders, along with everything inside themDelete the files in
deletedFilesApply the
renamesby moving each file or folder from itsbeforepath to itsafterpath. A rename whoseafterpath is inside.trashmeans the item was moved to the trashUpsert the files as main if they're in
createdMainFilesornullOrDraftToMainFilesUpsert the files as draft if they're in
createdDraftFilesormainOrNullToDraftFilesUpsert the files as null if they're in
createdNullFilesordraftOrMainToNullFilesDelete and reinsert the asset if it's in
modifiedMainFiles(as main) ormodifiedDraftFiles(as draft)Finally create the folders in
createdFolders
You should end up with all of your files in sync
Order matters for folders. For example, if you move a folder out and then delete its parent, applying the operations in the wrong order can accidentally delete all files under the directory and then "attempt and fail" to move the files in it out of it — so follow the order above, and if your system creates folders implicitly, it's generally safer to create a folder when a file needs it and delete it once it goes empty.
Last updated
Was this helpful?

