Public sharing in private repositories

christianhgross edited this page Apr 5, 2013 · 8 revisions

Here is a workaround to publicly share archives in a private repository on a Linux server. The only prerequisite is to have installed a web server like Apache2.

First, on your SparkleShare client, add a directory into your repository like for example a folder named "Public". At the end of this tutorial, all the archives of the "Public" folder will be available via the apache web server.

Second, on your GIT server, clone your repository somewhere (for example in your home directory) This can be done with the command:

git clone /path/to/the/repository

You can remove all the directories unless "Public". Let's say that the Public folder is located in "/home/jon/MyRepo"

Now we have to make this directory available to the Apache web server. There are several ways to do that: a link, an alias... But Be careful! if you decided to clone the GIT repository inside your home directory do not allow the server to show your home! Let's say the public folder is accesible via: http://www.example.com/public

And now the last step. If you drop a file on the public folder of the SparkleShare client, this file will not be available in /home/jon/MyRepo/Public until you run a git checkout on your server. Doing it manually is not an option, but we can use cron to automatically run the checkout every minute. In order to do that, just create the following script in your home directory, let's say we name it "update_public.sh":

#!/bin/sh    

cd /home/jon/MyRepo    
git fetch origin    
git reset origin/master    
git checkout -f master -- Public    
git clean -f    

And then we add the following line at the end of /etc/crontab file:

*  *    * * *   jon   /home/jon/update_public.sh >/dev/null 2>&1    

Now restart the cron service:

sudo /etc/init.d/cron restart

And that's all! Now, if you drop a file "example.txt" in the public folder of your SparkleShare client, it will be available (at most one minute later) http://www.jonserver.com/public/example.txt

Automatic file link generation (Mac only)

If your SparkleShare client run on a Mac you can automatically generate and send to the clipboard the link of the file you drop on the public folder, so you can paste it anywhere. This can be done easily with Mac folder actions and a simple apple script.

First, we are going to add a new folder action script. Open the AppleScript Editor and create the following script (change the url near the end to yours):

on adding folder items to this_folder after receiving added_items
    try
        tell application "Finder"
            --get the name of the folder
            set the folder_name to the name of this_folder
        end tell

        set fist_item to item 1 of added_items
        tell application "System Events"
            --get the name of the first added file
            set the file_name to the name of fist_item
        end tell

        tell application "Finder"
            --construct the url and copy it to the clipboard 
            set the clipboard to "http://www.jonserver.com/public/" & the file_name
        end tell
        beep
    end try
end adding folder items to

Save the script into the built-in folder action scripts located in Macintosh HD -> Libraries -> Scripts -> Folder Action Scripts. Let's say we name it sparkleshare - generate link.scpt.

Now right-click on your "Public" folder and select Configure Folder Actions. This will launch an AppleScript utility called Folder Actions Setup. Notice a checkbox titled "Enable Folder Actions", select this checkbox to turn on Folder Action. Then click on the (+) button on the right hand size of the window and select sparkleshare - generate link.scpt.

Now, every time you drop a file on the "Public" folder, the clipboard will contain the public link so you will be able to paste it (cmd+v) easily.

Implementing Server Side Logic

While Sparkleshare is a Git repository and as illustrated in the beginning of this page you can use Git itself to pull and push the sources. The only problem with this approach is that there is a delay between pushes and pulls. The solution lies with the notification system of Sparkleshare. Sparkleshare uses the fanout server to broadcast notifications between clients to indicate changes to a repository. A developer can hook into this mechanism making it possible for server side applications to push and pull in the same manner as any other client.

In specific the code that illustrates the technique is shown in the git hook project. https://github.com/hbons/sparkleshare-git-hook/blob/master/post-update. A developer would then need to execute the git logic as illustrated by the following shell code to push changes:

CHANNEL=$(git show HEAD:.sparkleshare)
MESSAGE=$(git rev-list HEAD | head -n 1)

And to receive changes the developer has to listen for similar messages. To make the pull work properly before the third party server application can receive the messages they need to subscribe to them as per the channel shell code.