From c2099e068ff74b12bec9159afe3773905be1fefb Mon Sep 17 00:00:00 2001
From: Dean Sheather <dean@deansheather.com>
Date: Sat, 27 May 2017 15:45:45 +1000
Subject: [PATCH] bump version to 1.0.1, change default bucket ID to public,
 change object schema

---
 cdn-origin.go |  6 ++++--
 objects.sql   | 27 ++++++++++++++-------------
 2 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/cdn-origin.go b/cdn-origin.go
index a651c3e..395024c 100644
--- a/cdn-origin.go
+++ b/cdn-origin.go
@@ -132,7 +132,7 @@ func main() {
 	log.Info("Attempting to listen on " + listenAddr)
 	server := &fasthttp.Server{
 		Handler:                       h,
-		Name:                          "whats-this/cdn-origin/1.0.0",
+		Name:                          "whats-this/cdn-origin/1.0.1",
 		ReadBufferSize:                1024 * 6, // 6 KB
 		ReadTimeout:                   time.Minute * 30,
 		WriteTimeout:                  time.Minute * 30,
@@ -167,7 +167,9 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
 	var content_type sql.NullString
 	var long_url sql.NullString
 	var object_type int
-	err := db.QueryRow(`SELECT backend_file_id, content_type, long_url, "type" FROM objects WHERE bucket='0' AND "key"=$1 LIMIT 1`, string(ctx.Path())).Scan(&backend_file_id, &content_type, &long_url, &object_type)
+	err := db.QueryRow(
+		`SELECT backend_file_id, content_type, long_url, "type" FROM objects WHERE bucket_key=$1 LIMIT 1`,
+		fmt.Sprintf("public%s", ctx.Path())).Scan(&backend_file_id, &content_type, &long_url, &object_type)
 	switch {
 	case err == sql.ErrNoRows:
 		ctx.SetStatusCode(fasthttp.StatusNotFound)
diff --git a/objects.sql b/objects.sql
index f16fbec..c939595 100644
--- a/objects.sql
+++ b/objects.sql
@@ -1,38 +1,39 @@
 -- "objects" table schema
 CREATE TABLE IF NOT EXISTS objects (
-  id VARCHAR(20) PRIMARY KEY NOT NULL, -- uint64 Twitter snowflake ID (primary)
-  bucket VARCHAR(20) NOT NULL, -- uint64 bucket ID (0 = public)
-  "key" VARCHAR(1024) NOT NULL, -- Full bucket path to file (including directory)
   bucket_key VARCHAR(1088) NOT NULL UNIQUE, -- bucket + key (unique)
+  bucket VARCHAR(20) NOT NULL, -- uint64 bucket ID ("public" for public bucket)
+  "key" VARCHAR(1024) NOT NULL, -- Full bucket path to file (including directory)
+  dir VARCHAR(1024) NOT NULL, -- Directory of file (with trailing slash)
   "type" integer NOT NULL DEFAULT 0, -- Object type enumerable (0 = file, 1 = short_url)
   backend_file_id VARCHAR(33) DEFAULT NULL, -- SeaweedFS file ID (only when object.type == 0)
   long_url VARCHAR(1024) DEFAULT NULL, -- Long URL (only when object.type == 1)
   content_type VARCHAR(255) DEFAULT 'application/octet-stream', -- Content-Type of file
+  content_length INT DEFAULT NULL, -- Content-Length of file
   auth_hash VARCHAR(64) DEFAULT NULL, -- Authentication hash: sha256(user.id + password + object.id)
   created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -- File creation timestamp
   md5_hash VARCHAR(32) DEFAULT NULL -- MD5 hash of file contents (or long URL)
 );
 
 -- Test file object: /index.md
-INSERT INTO objects (id, bucket, key, bucket_key, type, backend_file_id, content_type, md5_hash) VALUES (
-  '0',
-  '0',
+INSERT INTO objects (bucket_key, bucket, key, dir, type, backend_file_id, content_type, content_length, md5_hash) VALUES (
+  'public/index.txt',
+  'public',
   '/index.txt',
-  '0/index.txt',
+  '/',
   0,
   '1,020c3fd6ab',
   'text/plain',
+  0,
   'e2a81ac6617d7963bda5155239b4b262'
 );
 
 -- Test short_url object: /short_link
-INSERT INTO objects (id, bucket, key, bucket_key, type, long_url, content_type, md5_hash) VALUES (
-  '1',
-  '0',
+INSERT INTO objects (bucket_key, bucket, key, dir, type, long_url, content_type) VALUES (
+  'public/short_path',
+  'public',
   '/short_path',
-  '0/short_path',
+  '/',
   1,
   'https://google.com',
-  NULL,
-  '99999ebcfdb78df077ad2727fd00969f'
+  NULL
 );
-- 
GitLab