Home Download Docs Code Community
     1	/*
     2	Copyright 2013 The Perkeep Authors
     3	
     4	Licensed under the Apache License, Version 2.0 (the "License");
     5	you may not use this file except in compliance with the License.
     6	You may obtain a copy of the License at
     7	
     8	     http://www.apache.org/licenses/LICENSE-2.0
     9	
    10	Unless required by applicable law or agreed to in writing, software
    11	distributed under the License is distributed on an "AS IS" BASIS,
    12	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13	See the License for the specific language governing permissions and
    14	limitations under the License.
    15	*/
    16	
    17	// Package dummy is an example importer for development purposes.
    18	package dummy // import "perkeep.org/pkg/importer/dummy"
    19	
    20	import (
    21		"fmt"
    22		"log"
    23		"math/rand"
    24		"net/http"
    25		"net/url"
    26		"strconv"
    27		"strings"
    28	
    29		"perkeep.org/internal/httputil"
    30		"perkeep.org/pkg/blob"
    31		"perkeep.org/pkg/env"
    32		"perkeep.org/pkg/importer"
    33		"perkeep.org/pkg/schema"
    34	)
    35	
    36	func init() {
    37		if !env.IsDev() {
    38			// For this particular example importer, we only
    39			// register it if we're in "devcam server" mode.
    40			// Normally you'd avoid this check.
    41			return
    42		}
    43	
    44		// This Register call must happen during init.
    45		//
    46		// Register only registers an importer site type and not a
    47		// specific account on a site.
    48		importer.Register("dummy", &imp{})
    49	}
    50	
    51	// imp is the dummy importer, as a demo of how to write an importer.
    52	//
    53	// It must implement the importer.Importer interface in order for
    54	// it to be registered (in the init above).
    55	type imp struct {
    56		// The struct or underlying type implementing an importer
    57		// holds state that is global, and not per-account, so it
    58		// should not be used to cache account-specific
    59		// resources. Some importers (e.g. Foursquare) use this space
    60		// to cache mappings from site-specific global resource URLs
    61		// (e.g. category icons) to the fileref once it's been copied
    62		// into Perkeep.
    63	
    64	}
    65	
    66	func (*imp) Properties() importer.Properties {
    67		return importer.Properties{
    68			// NeedsAPIKey tells the importer framework that this
    69			// importer will be calling the
    70			// {RunContext,SetupContext}.Credentials method to get
    71			// the OAuth client ID & client secret, which may be
    72			// either configured on the importer permanode, or
    73			// statically in the server's config file.
    74			NeedsAPIKey: true,
    75	
    76			// SupportsIncremental signals to the importer host that this
    77			// importer has been optimized to be run regularly (e.g. every 5
    78			// minutes or half hour).  If it returns false, the user must
    79			// manually start imports.
    80			SupportsIncremental: false,
    81		}
    82	}
    83	
    84	const (
    85		acctAttrToken     = "my_token"
    86		acctAttrUsername  = "username"
    87		acctAttrRunNumber = "run_number" // some state
    88	)
    89	
    90	func (*imp) IsAccountReady(acct *importer.Object) (ready bool, err error) {
    91		// This method tells the importer framework whether this account
    92		// permanode (accessed via the importer.Object) is ready to start
    93		// an import.  Here you would typically check whether you have the
    94		// right metadata/tokens on the account.
    95		return acct.Attr(acctAttrToken) != "" && acct.Attr(acctAttrUsername) != "", nil
    96	}
    97	
    98	func (*imp) SummarizeAccount(acct *importer.Object) string {
    99		// This method is run by the importer framework if the account is
   100		// ready (see IsAccountReady) and summarizes the account in
   101		// the list of accounts on the importer page.
   102		return acct.Attr(acctAttrUsername)
   103	}
   104	
   105	func (*imp) ServeSetup(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) error {
   106		// ServeSetup gets called at the beginning of adding a new account
   107		// to an importer, or when an account is being re-logged into to
   108		// refresh its access token.
   109		// You typically start the OAuth redirect flow here.
   110		// The importer.OAuth2.RedirectURL and importer.OAuth2.RedirectState helpers can be used for OAuth2.
   111		http.Redirect(w, r, ctx.CallbackURL(), http.StatusFound)
   112		return nil
   113	}
   114	
   115	// Statically declare that our importer supports the optional
   116	// importer.ImporterSetupHTMLer interface.
   117	//
   118	// We do this in case importer.ImporterSetupHTMLer changes, or if we
   119	// typo the method name below. It turns this into a compile-time
   120	// error. In general you should do this in Go whenever you implement
   121	// optional interfaces.
   122	var _ importer.ImporterSetupHTMLer = (*imp)(nil)
   123	
   124	func (im *imp) AccountSetupHTML(host *importer.Host) string {
   125		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>"
   126	}
   127	
   128	func (im *imp) ServeCallback(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) {
   129		// ServeCallback is called after ServeSetup, at the end of an
   130		// OAuth redirect flow.
   131	
   132		code := r.FormValue("code") // e.g. get the OAuth code out of the redirect
   133		if code == "" {
   134			code = "some_dummy_code"
   135		}
   136		name := ctx.AccountNode.Attr(acctAttrUsername)
   137		if name == "" {
   138			names := []string{
   139				"alfred", "alice", "bob", "bethany",
   140				"cooper", "claire", "doug", "darla",
   141				"ed", "eve", "frank", "francine",
   142			}
   143			name = names[rand.Intn(len(names))]
   144		}
   145		if err := ctx.AccountNode.SetAttrs(
   146			"title", fmt.Sprintf("dummy account: %s", name),
   147			acctAttrUsername, name,
   148			acctAttrToken, code,
   149		); err != nil {
   150			httputil.ServeError(w, r, fmt.Errorf("Error setting attributes: %v", err))
   151			return
   152		}
   153		http.Redirect(w, r, ctx.AccountURL(), http.StatusFound)
   154	}
   155	
   156	func (im *imp) Run(ctx *importer.RunContext) (err error) {
   157		log.Printf("Running dummy importer.")
   158		defer func() {
   159			log.Printf("Dummy importer returned: %v", err)
   160		}()
   161		root := ctx.RootNode()
   162		fileRef, err := schema.WriteFileFromReader(ctx.Context(), ctx.Host.Target(), "foo.txt", strings.NewReader("Some file.\n"))
   163		if err != nil {
   164			return err
   165		}
   166		obj, err := root.ChildPathObject("foo.txt")
   167		if err != nil {
   168			return err
   169		}
   170		if err = obj.SetAttr("camliContent", fileRef.String()); err != nil {
   171			return err
   172		}
   173		n, _ := strconv.Atoi(ctx.AccountNode().Attr(acctAttrRunNumber))
   174		n++
   175		ctx.AccountNode().SetAttr(acctAttrRunNumber, fmt.Sprint(n))
   176		// Update the title each time, just to show it working. You
   177		// wouldn't actually do this:
   178		return root.SetAttr("title", fmt.Sprintf("dummy: %s import #%d", ctx.AccountNode().Attr(acctAttrUsername), n))
   179	}
   180	
   181	func (im *imp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
   182		httputil.BadRequestError(w, "Unexpected path: %s", r.URL.Path)
   183	}
   184	
   185	func (im *imp) CallbackRequestAccount(r *http.Request) (blob.Ref, error) {
   186		// We do not actually use OAuth, but this method works for us anyway.
   187		// Even if your importer implementation does not use OAuth, you can
   188		// probably just embed importer.OAuth1 in your implementation type.
   189		// If OAuth2, embedding importer.OAuth2 should work.
   190		return importer.OAuth1{}.CallbackRequestAccount(r)
   191	}
   192	
   193	func (im *imp) CallbackURLParameters(acctRef blob.Ref) url.Values {
   194		// See comment in CallbackRequestAccount.
   195		return importer.OAuth1{}.CallbackURLParameters(acctRef)
   196	}
Website layout inspired by memcached.
Content by the authors.