2013-10-19 22:48:05 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2013 The Perkeep Authors
|
2013-10-19 22:48:05 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package dummy is an example importer for development purposes.
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
package dummy // import "perkeep.org/pkg/importer/dummy"
|
2013-10-19 22:48:05 +00:00
|
|
|
|
|
|
|
import (
|
2013-10-19 23:59:46 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-04-01 18:47:03 +00:00
|
|
|
"math/rand"
|
2013-11-17 03:08:00 +00:00
|
|
|
"net/http"
|
2014-04-01 14:17:16 +00:00
|
|
|
"net/url"
|
2014-04-01 18:47:03 +00:00
|
|
|
"strconv"
|
2013-11-22 01:51:13 +00:00
|
|
|
"strings"
|
2013-10-19 23:59:46 +00:00
|
|
|
|
2018-01-03 05:03:30 +00:00
|
|
|
"perkeep.org/internal/httputil"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/blob"
|
|
|
|
"perkeep.org/pkg/env"
|
|
|
|
"perkeep.org/pkg/importer"
|
|
|
|
"perkeep.org/pkg/schema"
|
2013-10-19 22:48:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-04-02 12:55:01 +00:00
|
|
|
if !env.IsDev() {
|
2014-04-01 22:11:05 +00:00
|
|
|
// For this particular example importer, we only
|
|
|
|
// register it if we're in "devcam server" mode.
|
|
|
|
// Normally you'd avoid this check.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-01 18:47:03 +00:00
|
|
|
// This Register call must happen during init.
|
|
|
|
//
|
|
|
|
// Register only registers an importer site type and not a
|
|
|
|
// specific account on a site.
|
|
|
|
importer.Register("dummy", &imp{})
|
2013-10-19 22:48:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 18:47:03 +00:00
|
|
|
// imp is the dummy importer, as a demo of how to write an importer.
|
|
|
|
//
|
|
|
|
// It must implement the importer.Importer interface in order for
|
|
|
|
// it to be registered (in the init above).
|
|
|
|
type imp struct {
|
|
|
|
// The struct or underlying type implementing an importer
|
|
|
|
// holds state that is global, and not per-account, so it
|
|
|
|
// should not be used to cache account-specific
|
|
|
|
// resources. Some importers (e.g. Foursquare) use this space
|
|
|
|
// to cache mappings from site-specific global resource URLs
|
|
|
|
// (e.g. category icons) to the fileref once it's been copied
|
|
|
|
// into Camlistore.
|
|
|
|
|
2013-10-19 22:48:05 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 18:34:23 +00:00
|
|
|
func (*imp) SupportsIncremental() bool {
|
|
|
|
// SupportsIncremental signals to the importer host that this
|
|
|
|
// importer has been optimized to be run regularly (e.g. every 5
|
|
|
|
// minutes or half hour). If it returns false, the user must
|
|
|
|
// manually start imports.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-04-01 18:47:03 +00:00
|
|
|
func (*imp) NeedsAPIKey() bool {
|
|
|
|
// This tells the importer framework that we our importer will
|
|
|
|
// be calling the {RunContext,SetupContext}.Credentials method
|
|
|
|
// to get the OAuth client ID & client secret, which may be
|
|
|
|
// either configured on the importer permanode, or statically
|
|
|
|
// in the server's config file.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
acctAttrToken = "my_token"
|
|
|
|
acctAttrUsername = "username"
|
|
|
|
acctAttrRunNumber = "run_number" // some state
|
|
|
|
)
|
|
|
|
|
|
|
|
func (*imp) IsAccountReady(acct *importer.Object) (ready bool, err error) {
|
|
|
|
// This method tells the importer framework whether this account
|
|
|
|
// permanode (accessed via the importer.Object) is ready to start
|
|
|
|
// an import. Here you would typically check whether you have the
|
|
|
|
// right metadata/tokens on the account.
|
|
|
|
return acct.Attr(acctAttrToken) != "" && acct.Attr(acctAttrUsername) != "", nil
|
2013-10-19 22:48:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 18:47:03 +00:00
|
|
|
func (*imp) SummarizeAccount(acct *importer.Object) string {
|
|
|
|
// This method is run by the importer framework if the account is
|
|
|
|
// ready (see IsAccountReady) and summarizes the account in
|
|
|
|
// the list of accounts on the importer page.
|
|
|
|
return acct.Attr(acctAttrUsername)
|
2013-10-19 23:59:46 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 18:47:03 +00:00
|
|
|
func (*imp) ServeSetup(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) error {
|
|
|
|
// ServeSetup gets called at the beginning of adding a new account
|
|
|
|
// to an importer, or when an account is being re-logged into to
|
|
|
|
// refresh its access token.
|
|
|
|
// You typically start the OAuth redirect flow here.
|
2014-05-14 23:09:20 +00:00
|
|
|
// The importer.OAuth2.RedirectURL and importer.OAuth2.RedirectState helpers can be used for OAuth2.
|
2014-04-01 18:47:03 +00:00
|
|
|
http.Redirect(w, r, ctx.CallbackURL(), http.StatusFound)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Statically declare that our importer supports the optional
|
|
|
|
// importer.ImporterSetupHTMLer interface.
|
|
|
|
//
|
|
|
|
// We do this in case importer.ImporterSetupHTMLer changes, or if we
|
|
|
|
// typo the method name below. It turns this into a compile-time
|
|
|
|
// error. In general you should do this in Go whenever you implement
|
|
|
|
// optional interfaces.
|
|
|
|
var _ importer.ImporterSetupHTMLer = (*imp)(nil)
|
|
|
|
|
|
|
|
func (im *imp) AccountSetupHTML(host *importer.Host) string {
|
|
|
|
return "<h1>Hello from the dummy importer!</h1><p>I am example HTML. This importer is a demo of how to write an importer.</p>"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imp) ServeCallback(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) {
|
|
|
|
// ServeCallback is called after ServeSetup, at the end of an
|
|
|
|
// OAuth redirect flow.
|
|
|
|
|
|
|
|
code := r.FormValue("code") // e.g. get the OAuth code out of the redirect
|
|
|
|
if code == "" {
|
|
|
|
code = "some_dummy_code"
|
|
|
|
}
|
|
|
|
name := ctx.AccountNode.Attr(acctAttrUsername)
|
|
|
|
if name == "" {
|
|
|
|
names := []string{
|
|
|
|
"alfred", "alice", "bob", "bethany",
|
|
|
|
"cooper", "claire", "doug", "darla",
|
|
|
|
"ed", "eve", "frank", "francine",
|
|
|
|
}
|
|
|
|
name = names[rand.Intn(len(names))]
|
|
|
|
}
|
|
|
|
if err := ctx.AccountNode.SetAttrs(
|
|
|
|
"title", fmt.Sprintf("dummy account: %s", name),
|
|
|
|
acctAttrUsername, name,
|
|
|
|
acctAttrToken, code,
|
|
|
|
); err != nil {
|
|
|
|
httputil.ServeError(w, r, fmt.Errorf("Error setting attributes: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, ctx.AccountURL(), http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imp) Run(ctx *importer.RunContext) (err error) {
|
2013-11-22 01:51:13 +00:00
|
|
|
log.Printf("Running dummy importer.")
|
|
|
|
defer func() {
|
|
|
|
log.Printf("Dummy importer returned: %v", err)
|
|
|
|
}()
|
2014-04-01 18:47:03 +00:00
|
|
|
root := ctx.RootNode()
|
|
|
|
fileRef, err := schema.WriteFileFromReader(ctx.Host.Target(), "foo.txt", strings.NewReader("Some file.\n"))
|
2013-11-22 01:51:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-10-19 23:59:46 +00:00
|
|
|
}
|
2014-04-01 18:47:03 +00:00
|
|
|
obj, err := root.ChildPathObject("foo.txt")
|
2013-11-22 01:51:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-01 18:47:03 +00:00
|
|
|
if err = obj.SetAttr("camliContent", fileRef.String()); err != nil {
|
2013-11-22 01:51:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-04-01 18:47:03 +00:00
|
|
|
n, _ := strconv.Atoi(ctx.AccountNode().Attr(acctAttrRunNumber))
|
|
|
|
n++
|
|
|
|
ctx.AccountNode().SetAttr(acctAttrRunNumber, fmt.Sprint(n))
|
|
|
|
// Update the title each time, just to show it working. You
|
|
|
|
// wouldn't actually do this:
|
|
|
|
return root.SetAttr("title", fmt.Sprintf("dummy: %s import #%d", ctx.AccountNode().Attr(acctAttrUsername), n))
|
2013-10-19 22:48:05 +00:00
|
|
|
}
|
2013-11-17 03:08:00 +00:00
|
|
|
|
|
|
|
func (im *imp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
httputil.BadRequestError(w, "Unexpected path: %s", r.URL.Path)
|
|
|
|
}
|
2014-05-14 23:09:20 +00:00
|
|
|
|
|
|
|
func (im *imp) CallbackRequestAccount(r *http.Request) (blob.Ref, error) {
|
|
|
|
// We do not actually use OAuth, but this method works for us anyway.
|
|
|
|
// Even if your importer implementation does not use OAuth, you can
|
|
|
|
// probably just embed importer.OAuth1 in your implementation type.
|
|
|
|
// If OAuth2, embedding importer.OAuth2 should work.
|
|
|
|
return importer.OAuth1{}.CallbackRequestAccount(r)
|
|
|
|
}
|
|
|
|
|
2014-04-01 14:17:16 +00:00
|
|
|
func (im *imp) CallbackURLParameters(acctRef blob.Ref) url.Values {
|
2014-05-14 23:09:20 +00:00
|
|
|
// See comment in CallbackRequestAccount.
|
|
|
|
return importer.OAuth1{}.CallbackURLParameters(acctRef)
|
|
|
|
}
|