Beispiel #1
0
 def full_path_for_part(self, partnumber):
     if not partnumber:
         raise exceptions.NoFileException("Invalid part")
     try:
         partnumber = int(partnumber)
     except ValueError:
         raise exceptions.NoFileException("Invalid part")
     if partnumber > self.num_parts:
         raise exceptions.NoFileException("partnumber is greater than number of parts")
     return os.path.join(self.directory(), self.filename_for_part(partnumber))
Beispiel #2
0
def docserver_get_mp3_url(documentid):
    try:
        document = models.Document.objects.get(external_identifier=documentid)
    except models.Document.DoesNotExist:
        raise exceptions.NoFileException()
    part = document.get_file("mp3")
    return part.get_absolute_url("ds-download-mp3")
Beispiel #3
0
def docserver_get_url(documentid, slug, subtype=None, part=None, version=None):
    try:
        document = models.Document.objects.get(external_identifier=documentid)
    except models.Document.DoesNotExist:
        raise exceptions.NoFileException()
    thefile = document.get_file(slug, subtype, part, version)
    return thefile.get_absolute_url(partnumber=part)
Beispiel #4
0
def docserver_get_filename(documentid, slug, subtype=None, part=1, version=None):
    try:
        document = models.Document.objects.get(external_identifier=documentid)
    except models.Document.DoesNotExist:
        raise exceptions.NoFileException()
    result = document.get_file(slug, subtype, part, version)
    if isinstance(result, models.SourceFile):
        return result.fullpath
    else:
        return result.full_path_for_part(part)
Beispiel #5
0
 def get_absolute_url(self, partnumber=None):
     url = reverse(
         "ds-download-external",
         args=[self.document.external_identifier, self.module_version.module.slug])
     v = self.module_version.version
     sub = self.outputname
     url = "%s?v=%s&subtype=%s" % (url, v, sub)
     if partnumber:
         try:
             partnumber = int(partnumber)
         except ValueError:
             raise exceptions.NoFileException("Invalid part")
         url = "%s&part=%s" % (url, partnumber)
     return url
Beispiel #6
0
def docserver_get_wav_filename(documentid):
    """ Return a tuple (filename, created) containing the filename
        of a wave file for this document. If created is True, it means
        the file was generated on demand and you must delete it when
        you're finished. Otherwise it's from the docserver
    """
    try:
        filename = docserver_get_filename(documentid, "wav", "wave")
        if not os.path.exists(filename):
            raise exceptions.NoFileException("Wave file doesn't exist")
        return filename, False
    except:  # Error getting file because it's not in the db or it doesn't exist
        print("Error getting file, calculating again")
        mp3filename = docserver_get_filename(documentid, "mp3")
        fp, tmpname = tempfile.mkstemp(".wav")
        os.close(fp)
        proclist = ["lame", "--decode", mp3filename, tmpname]
        p = subprocess.Popen(proclist)
        p.communicate()
        return tmpname, True
Beispiel #7
0
    def get_file(self, slug, subtype=None, part=None, version=None):
        try:
            sourcetype = SourceFileType.objects.get_by_slug(slug)
        except SourceFileType.DoesNotExist:
            sourcetype = None

        if sourcetype:
            files = self.sourcefiles.filter(file_type=sourcetype)
            if len(files) == 0:
                raise exceptions.NoFileException("Looks like a sourcefile, but I can't find one")
            else:
                return files[0]

        try:
            module = Module.objects.get(slug=slug)
        except Module.DoesNotExist:
            raise exceptions.NoFileException("Cannot find a module with type %s" % slug)
        moduleversions = module.versions
        if version:
            moduleversions = moduleversions.filter(version=version)
        else:
            moduleversions = moduleversions.order_by("-date_added")
        if len(moduleversions) == 0:
            raise exceptions.NoFileException("No known versions for this module")

        dfs = None
        for mv in moduleversions:
            # go through all the versions until we find a file of that version
            # If we have a more recent version, but only a derived file for an older
            # version, return the older version.
            dfs = self.derivedfiles.filter(module_version=mv).all()
            if subtype:
                dfs = dfs.filter(outputname=subtype)
            if dfs.count() > 0:
                # We found some files, break
                break
        if dfs.count() > 1:
            raise exceptions.TooManyFilesException(
                "Found more than 1 subtype for this module but you haven't specified what you want")
        elif dfs.count() == 1:
            # Double-check if subtypes match. This is to catch the case where we
            # have only one subtype for a type but we don't specify it in the
            # query. By 'luck' we will get the right subtype, but this doesn't
            # preclude the default subtype changing in a future version.
            # Explicit is better than implicit
            derived = dfs.get()
            if derived.outputname != subtype:
                raise exceptions.NoFileException(
                    "This module has only one subtype which you must specify (%s)" % (derived.outputname,))
            # Select the part.
            # If the file has many parts and ?part is not set then it's an error
            if part:
                try:
                    part = int(part)
                except ValueError:
                    raise exceptions.NoFileException("Invalid part")
                if part > derived.num_parts:
                    raise exceptions.NoFileException("Invalid part")

            if derived.num_parts == 0:
                raise exceptions.NoFileException("No parts on this file")
            elif derived.num_parts > 1 and not part:
                raise exceptions.TooManyFilesException("Found more than 1 part without part set")
            else:
                return derived
        else:
            # If no files, or none with this version
            msg = "No derived files with this type/subtype"
            if version:
                msg += " or version"
            raise exceptions.NoFileException(msg)