Exemplo n.º 1
0
def setup_dirs():
    """make voice directories if they don't exist already."""

    outDir = dsh_config.lookup('voice_data_dir_out')

    if not dsh_utils.try_mkdir_ifdoesnt_exist(outDir,
                                              'dsh_simple1.setup_dirs: ' +
                                              'outgoing dir '):
        return (False,None,None)

    miscDict = dsh_utils.get_misc_dict()
    yearMonth = miscDict['year_month']

    inDir = dsh_config.lookup('voice_data_dir_in')
    inDir = os.path.join(inDir, yearMonth)
    
    if not dsh_utils.try_mkdir_ifdoesnt_exist(inDir,
                                              'dsh_simple1.setup_dirs: ' +
                                              'incoming dir '):
        return (False,None,None)

    return (True,outDir,inDir)
Exemplo n.º 2
0
def make_full_unique_filename(dshUid,
                              uploadedFileName,
                              phoneNumber='',
                              orgAlias='',
                              name='',
                              startWithRoot=False,
                              uploadType='django_voice'):
    """when called by Asterisk, startWithRoot=True
    when called by django, startWithRoot=False.
    for django, it looks like this:
    voice/09/07/090727_195346_88888888_test4.mp3
    for asterisk, it looks like this:
    /u/rywang/phone_data/django/media/voice/09/07/090727_195346_88888888_123_alias_name_test4.mp3
    """

    if uploadedFileName:
        uploadedFileName = dsh_utils.strip_join_str(uploadedFileName)

    #
    # first determine the upper-level directory.
    # 
    if startWithRoot:
        #
        # called by Asterisk.
        # it's this: /u/rywang/phone_data/django/media/voice/
        # like:
        # /u/rywang/phone_data/django/media/voice/
        # or
        # voice/
        #
        inDir = dsh_config.lookup('VOICE_DIR')
    else:
        #
        # called by django.
        # from dsh_django_utils.py
        # it's this: voice/
        #
        if uploadType == 'django_voice':
            inDir = dsh_config.lookup('VOICE_SUBDIR')
        elif uploadType == 'django_image':
            inDir = dsh_config.lookup('IMG_SUBDIR')
        else:
            return None


    #
    # mid-level directory named after year and month.
    # like
    # 09/07/
    #
    dateStrFormat = dsh_config.lookup('DATE_FORMAT_DIR_STR')
    yearMonthSubDir = dsh_utils.get_misc_dict()['now'].strftime(dateStrFormat)


    #
    # finally leaf-level file name.
    #
    if uploadType == 'asterisk':
        #
        # this is called by asterisk.
        #
        fileName = make_unique_voice_filename_asterisk(
            dshUid, phoneNumber, orgAlias, name)
    elif uploadType == 'django_image':
        #
        # called by dsh_django_utils.make_uploaded_image_name()
        #
        fileName = make_unique_image_filename_django(
            dshUid, name, uploadedFileName)
    elif uploadType == 'django_voice':
        #
        # called by dsh_django_utils.make_uploaded_file_name()
        #
        fileName = make_unique_voice_filename_django(
            dshUid, uploadedFileName)
    else:
        return None
    
    return os.path.join(inDir, yearMonthSubDir, fileName)