tour: improve local storage handling of files

this supports adding new pages or removing them without invalidating
local caches of files that didn't change

Fixes #13252

Change-Id: Ibcc400347551de7f5ed1e58250a9c1cbb437cbaa
Reviewed-on: https://go-review.googlesource.com/17407
Reviewed-by: Chris Broadfoot <cbro@golang.org>
diff --git a/gotour/tour.go b/gotour/tour.go
index a9b19ab..0d6ea3b 100644
--- a/gotour/tour.go
+++ b/gotour/tour.go
@@ -6,6 +6,8 @@
 
 import (
 	"bytes"
+	"crypto/sha1"
+	"encoding/base64"
 	"encoding/json"
 	"fmt"
 	"html/template"
@@ -96,6 +98,7 @@
 type File struct {
 	Name    string
 	Content string
+	Hash    string
 }
 
 // Page defines the JSON form of a tour lesson page.
@@ -145,6 +148,8 @@
 			f := &p.Files[i]
 			f.Name = c.FileName
 			f.Content = string(c.Raw)
+			hash := sha1.Sum(c.Raw)
+			f.Hash = base64.StdEncoding.EncodeToString(hash[:])
 		}
 	}
 
diff --git a/static/js/controllers.js b/static/js/controllers.js
index 3a31632..157881e 100755
--- a/static/js/controllers.js
+++ b/static/js/controllers.js
@@ -22,8 +22,7 @@
                 var f = file();
                 return f && f.Content;
             }, function(val) {
-                var key = $scope.lessonId + '.' + ($scope.curPage - 1) + '.' + $scope.curFile;
-                storage.set(key, val);
+                storage.set(file().Hash, val);
             });
         });
 
diff --git a/static/js/services.js b/static/js/services.js
index 91a08c9..1152da6 100755
--- a/static/js/services.js
+++ b/static/js/services.js
@@ -189,7 +189,7 @@
                             var page = lesson.Pages[p];
                             for (var f = 0; f < page.Files.length; f++) {
                                 page.Files[f].OrigContent = page.Files[f].Content;
-                                var val = storage.get(lessonName + '.' + p + '.' + f);
+                                var val = storage.get(page.Files[f].Hash);
                                 if (val !== null) {
                                     page.Files[f].Content = val;
                                 }