<commit_before>package main

// secretshare server - mediate access to Amazon S3 by secretshare client
// Copyright (C) 2016  <NAME>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

import (
	"crypto/rand"
	"encoding/hex"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"

	"github.com/codegangsta/cli"
	"github.com/waucka/secretshare/commonlib"
)

var (
	ErrIDGen   = errors.New("Failed to generate random ID!")
	ErrIDShort = errors.New("Not enough random bytes for ID!  This should never happen!")
	ErrPreSign = errors.New("Failed to generate pre-signed upload URL!")

	Version           = 2 //deploy.sh:VERSION
	DefaultConfigPath = "/etc/secretshare-server.json"
)

type serverConfig struct {
	ListenAddr string `json:"addr"`
	ListenPort int    `json:"port"`
	SecretKey  string `json:"secret_key"`
}

func generateId() (string, error) {
	idbin := make([]byte, 32)
	num_id_bytes, err := rand.Read(idbin)
	if err != nil {
		return "", ErrIDGen
	}
	if num_id_bytes < 32 {
		return "", ErrIDShort
	}
	return hex.EncodeToString(idbin), nil
}

func generateSignedURL(svc *s3.S3, id, prefix string, ttl time.Duration) (string, http.Header, error) {
	s3key := prefix + id

	putObjectInput := &s3.PutObjectInput{
		Bucket:      &commonlib.Bucket,
		Key:         &s3key,
		Expires:     aws.Time(time.Now().Add(ttl)),<commit_msg>Express file ID in base 64 for reduced length<commit_after>package main

// secretshare server - mediate access to Amazon S3 by secretshare client
// Copyright (C) 2016  <NAME>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

import (
	"crypto/rand"
	"encoding/base64"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"

	"github.com/codegangsta/cli"
	"github.com/waucka/secretshare/commonlib"
)

var (
	ErrIDGen   = errors.New("Failed to generate random ID!")
	ErrIDShort = errors.New("Not enough random bytes for ID!  This should never happen!")
	ErrPreSign = errors.New("Failed to generate pre-signed upload URL!")

	// We use a custom base-64 encoding because:
	//
	//   * '/' and '=' tend to introduce line breaks or breaks in text selection
	//   * '/' is the path separator in S3
	Encoding = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxzy0123456789+_").WithPadding(base64.NoPadding)

	Version           = 2 //deploy.sh:VERSION
	DefaultConfigPath = "/etc/secretshare-server.json"
)

type serverConfig struct {
	ListenAddr string `json:"addr"`
	ListenPort int    `json:"port"`
	SecretKey  string `json:"secret_key"`
}

func generateId() (string, error) {
	idbin := make([]byte, 32)
	num_id_bytes, err := rand.Read(idbin)
	if err != nil {
		return "", ErrIDGen
	}
	if num_id_bytes < 32 {
		return "", ErrIDShort
	}
	return Encoding.EncodeToString(idbin), nil
}

func generateSignedURL(svc *s3.S3, id, prefix string, ttl time.Duration) (string, http.Header, error) {
	s3key := prefix + id

	putObjectInput := &s3.PutObjectInput{
		Bucket:      &commonlib.Bucket,
		Key:         &s3key,
		Expires:     aws.Time(time.Now().Add(ttl)),
{'fraction_non_alphanumeric': 0.12317596566523605, 'fraction_numerical': 0.011802575107296138, 'mean_word_length': 3.684422110552764, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2524002', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1541, 'n_words': 479}