<commit_before>package gitmediaclient

import (
	".."
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
)

func Put(filename string) error {
	oid := filepath.Base(filename)
	stat, err := os.Stat(filename)
	if err != nil {
		return err
	}

	file, err := os.Open(filename)
	if err != nil {
		return err
	}

	req, creds, err := clientRequest("PUT", oid)
	if err != nil {
		return err
	}

	req.Body = file
	req.ContentLength = stat.Size()

	res, err := doRequest(req, creds)
	if err != nil {
		return err
	}

	fmt.Printf("Sending %s from %s: %d\n", oid, filename, res.StatusCode)
	return nil
}

func Get(filename string) (io.ReadCloser, error) {
	oid := filepath.Base(filename)
	if stat, err := os.Stat(filename); err != nil || stat == nil {
		req, creds, err := clientRequest("GET", oid)
		if err != nil {
			return nil, err
		}

		req.Header.Set("Accept", "application/vnd.git-media")
		res, err := doRequest(req, creds)

		if err != nil {
			return nil, err
		}

		return res.Body, nil<commit_msg>When the client gets media and the media file doesn't exist, ensure it is created<commit_after>package gitmediaclient

import (
	".."
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
)

func Put(filename string) error {
	oid := filepath.Base(filename)
	stat, err := os.Stat(filename)
	if err != nil {
		return err
	}

	file, err := os.Open(filename)
	if err != nil {
		return err
	}

	req, creds, err := clientRequest("PUT", oid)
	if err != nil {
		return err
	}

	req.Body = file
	req.ContentLength = stat.Size()

	res, err := doRequest(req, creds)
	if err != nil {
		return err
	}

	fmt.Printf("Sending %s from %s: %d\n", oid, filename, res.StatusCode)
	return nil
}

func Get(filename string) (io.ReadCloser, error) {
	oid := filepath.Base(filename)
	if stat, err := os.Stat(filename); err != nil || stat == nil {
		req, creds, err := clientRequest("GET", oid)
		if err != nil {
			return nil, err
		}

		req.Header.Set("Accept", "application/vnd.git-media")
		res, err := doRequest(req, creds)
		defer res.Body.Close()

		if err != nil {
			return nil, err
		}

		mediafile, err := os.Create(filename)
		defer mediafile.Close()

		if err != nil {
			return nil, err
		}

		_, err = io.Copy(mediafile, res.Body)
		if err != nil {
			return nil, err
		}
{'fraction_non_alphanumeric': 0.15627668659265584, 'fraction_numerical': 0.0017079419299743809, 'mean_word_length': 3.2755474452554743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3692531', 'n_tokens_mistral': 953, 'n_tokens_neox': 888, 'n_words': 246}