<commit_before>import hashlib

from rest.models import Sound
from rest.serializers import SoundSerializer
from rest_framework import generics


def hashfile(afile, hasher, blocksize=65536):
    buf = afile.read(blocksize)
    while len(buf) > 0:
        hasher.update(buf)
        buf = afile.read(blocksize)
    return hasher.digest()


class SoundList(generics.ListCreateAPIView):
    queryset = Sound.objects.all()
    serializer_class = SoundSerializer

    def perform_create(self, serializer):
        sound = self.request._files['sound']
        codec = sound.content_type.split('/')[-1]
        size = sound._size
        duration = 0.0  # TODO
        tempfile = self.request._files['sound'].file.name
        sha1 = hashfile(open(tempfile, 'rb'), hashlib.sha1()).hex()
        # TODO: validate calculated parameters before saving
        # TODO: if file already uploaded, do not save
        serializer.save(codec=codec, size=size, duration=duration, sha1=sha1)


class SoundDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Sound.objects.all()
    serializer_class = SoundSerializer<commit_msg>Rename uploaded files to their SHA1<commit_after>import hashlib

from rest.models import Sound
from rest.serializers import SoundSerializer
from rest_framework import generics


def hashfile(afile, hasher, blocksize=65536):
    buf = afile.read(blocksize)
    while len(buf) > 0:
        hasher.update(buf)
        buf = afile.read(blocksize)
    return hasher.digest()


class SoundList(generics.ListCreateAPIView):
    queryset = Sound.objects.all()
    serializer_class = SoundSerializer

    def perform_create(self, serializer):
        sound = self.request._files['sound']
        codec = sound.content_type.split('/')[-1]
        size = sound._size
        duration = 0.0  # TODO
        tempfile = self.request._files['sound'].file.name
        sha1 = hashfile(open(tempfile, 'rb'), hashlib.sha1()).hex()
        self.request._files['sound']._name = sha1
        # TODO: validate calculated parameters before saving
        # TODO: if file already uploaded, do not save
        serializer.save(codec=codec, size=size, duration=duration, sha1=sha1)


class SoundDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Sound.objects.all()
    serializer_class = SoundSerializer
{'fraction_non_alphanumeric': 0.10320901994796183, 'fraction_numerical': 0.012142237640936688, 'mean_word_length': 3.4883268482490273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '292610', 'n_tokens_mistral': 688, 'n_tokens_neox': 662, 'n_words': 158}