def __setitem__(self, item, value): """ 修改一个系统配置项的值到用户个性化设置中,这主要用于系统运行期保存一些个性化的参数设置。 """ if self.__getitem__(item) == value: return if item in self._cache: self._cache.pop(item) op = threadlocals.get_current_user() if op and op.is_anonymous(): op = None items = item.split(".", 1) opt = Option.objects.get(app_label=items[0], name=items[1]) try: p_opt = PersonalOption.objects.get(option=opt, user=op) except ObjectDoesNotExist: p_opt = PersonalOption(option=opt, user=op) #create it if not exists if not (p_opt.value == value): p_opt.value = value p_opt.save() if item == 'base.language': from django.utils.translation import check_for_language, activate, get_language request = threadlocals.get_current_request() if request: lang_code = value if lang_code and check_for_language(lang_code): if hasattr(request, 'session'): request.session['django_language'] = lang_code activate(lang_code) request.LANGUAGE_CODE = get_language() self._cache[item] = value
def clean(self, data, initial=None): """Audio field validation for file extension""" data = super(AudioField, self).clean(data, initial) request = threadlocals.get_current_request() filename = data.name ext = os.path.splitext(filename)[1] ext = ext.lower() if ext not in self.ext_whitelist: error_msg = _("not allowed filetype!") logger.error(error_msg) raise forms.ValidationError(error_msg) convert_to = int(request.POST["convert_type"]) ext = ext.split('.')[1] audio_type = CONVERT_TYPE_CHK[convert_to] error_msg = _("not allowed : file format conversion is not allowed for same audio type (except Wav)") if convert_to: if ext == audio_type and ext != 'wav': error_msg += ' %s format !!' % ext logger.error(error_msg) raise forms.ValidationError(error_msg) else: pass return data
def _rename_audio(self, instance=None, **kwargs): '''Rename uploaded audio file & calls methods to convert audio file format if convert_to is selected''' if getattr(instance, self.name): filename = getattr(instance, self.name).path #Get the extension and limit to 3 chars ext = os.path.splitext(filename)[1].lower()[:4] #Get new file name and make sure it's unique dst = self.generate_filename( instance, '%s%s%s' % (self.filename_prefix, self.uuid, ext)) dst_fullpath = os.path.join(settings.MEDIA_ROOT, dst) # Same file should not exits if not os.path.isfile(dst_fullpath): if os.path.abspath(filename) != os.path.abspath(dst_fullpath): os.rename(filename, dst_fullpath) self._convert_audio(dst_fullpath, instance, ext[1:4]) request = threadlocals.get_current_request() convert_type = int(request.POST["convert_type"]) # 0 => Keep original if convert_type > 0: #Delete original audio file if os.path.exists(dst_fullpath): #Check for no .. and no * #DISABLED Delete file """ if dst_fullpath.find('../../') == -1 and dst_fullpath.find('*') == -1: os.remove(dst_fullpath) """ ext = '.' + CONVERT_TYPE_CHK[convert_type] dst = self.generate_filename( instance, '%s%s%s' % (self.filename_prefix, self.uuid, ext)) setattr(instance, self.attname, dst) instance.save() else: error_msg = ("File already exists!") logger.error(error_msg)
def _rename_audio(self, instance=None, **kwargs): '''Rename uploaded audio file & calls methods to convert audio file format if convert_to is selected''' if getattr(instance, self.name): filename = getattr(instance, self.name).path #Get the extension and limit to 3 chars ext = os.path.splitext(filename)[1].lower()[:4] #Get new file name and make sure it's unique dst = self.generate_filename(instance, '%s%s%s' % (self.filename_prefix, self.uuid, ext)) dst_fullpath = os.path.join(settings.MEDIA_ROOT, dst) # Same file should not exits if not os.path.isfile(dst_fullpath): if os.path.abspath(filename) != os.path.abspath(dst_fullpath): os.rename(filename, dst_fullpath) self._convert_audio(dst_fullpath, instance, ext[1:4]) request = threadlocals.get_current_request() convert_type = int(request.POST["convert_type"]) # 0 => Keep original if convert_type > 0: #Delete original audio file if os.path.exists(dst_fullpath): #Check for no .. and no * #DISABLED Delete file """ if dst_fullpath.find('../../') == -1 and dst_fullpath.find('*') == -1: os.remove(dst_fullpath) """ ext = '.' + CONVERT_TYPE_CHK[convert_type] dst = self.generate_filename(instance, '%s%s%s' % (self.filename_prefix, self.uuid, ext)) setattr(instance, self.attname, dst) instance.save() else: error_msg = ("file already exists!") logger.error(error_msg)
def _convert_audio(self, filename, instance=None, ext=None): '''Convert uploaded audio file to selected format''' request = threadlocals.get_current_request() convert_type = int(request.POST["convert_type"]) channel_no = int(request.POST["channel_type"]) freq_value = int(request.POST["freq_type"]) splitted_filename = list(os.path.splitext(filename))[0] # converted filename without ext logger.debug("convert audio : %s->%s" % (str(ext), CONVERT_TYPE_CHK[convert_type])) filename_temp = filename[:-4] + '_temp' # 1) MP3 TO WAV if ext == 'mp3' and CONVERT_TYPE_CHK[convert_type] == 'wav': logger.debug("convert MP3 to WAV - channel %s freq: %s" % (str(channel_no), str(freq_value))) #prepare Sox parameters for Channels convertion conv_channel = "-s -c %s" % str(channel_no) if channel_no > 0 else '' #prepare Sox parameters for Frequency convertion conv_freq = "-r %s" % str(freq_value) if freq_value > 0 else '' conv = "sox %s %s %s %s.wav" % (filename, conv_freq, conv_channel, splitted_filename) result = audio_convert_task.delay(conv) logger.debug("Sox command :> %s" % conv) #TODO: CONVERT MP3 TO OGG # 2) MP3 TO OGG # not working if ext == 'mp3' and CONVERT_TYPE_CHK[convert_type] == 'ogg': logger.debug('MP3 to OGG') conv = "mpg321 %s -w raw && oggenc raw -o %s.ogg" % (filename, splitted_filename) #conv = "sox %s %s.ogg" % (filename, splitted_filename) #conv = "ffmpeg -i %s %s.ogg" % (filename, splitted_filename) result = audio_convert_task.delay(conv) logger.debug("command :> %s" % conv) # 3) WAV TO MP3 if ext == 'wav' and CONVERT_TYPE_CHK[convert_type] == 'mp3': logger.debug('WAV to MP3') #conv = "lame -V2 %s %s.mp3" % (filename, filename) #conv = "lame -h %s %s.mp3" % (filename, filename) conv = "sox %s %s.mp3" % (filename, splitted_filename) result = audio_convert_task.delay(conv) logger.debug("Sox command :> %s" % conv) # 3) WAV TO WAV if ext == 'wav' and CONVERT_TYPE_CHK[convert_type] == 'wav': logger.debug("convert WAV to WAV - channel %s freq: %s" % (str(channel_no), str(freq_value))) filename_temp = filename_temp + '.wav' #prepare Sox parameters for Channels convertion conv_channel = "-s -c %s" % str(channel_no) if channel_no > 0 else '' #prepare Sox parameters for Frequency convertion conv_freq = "-r %s" % str(freq_value) if freq_value > 0 else '' conv = "sox %s %s %s %s.wav" % (filename_temp, conv_freq, conv_channel, splitted_filename) #cmd = 'sox /usr/share/newfies/../newfies/usermedia/upload/audiofiles/audio-file-XFPQN-6216731785_temp.wav -r 8000 -s -c 1 /usr/share/newfies/../newfies/usermedia/upload/audiofiles/audio-file-XFPQN-6216731785.wav' #print "first file converted!" #create a temp copy of the file shutil.copy2(filename, filename_temp) result = audio_convert_task.delay(conv) logger.debug("result :> %s" % str(result)) logger.debug("command :> %s" % conv) # 4) WAV TO OGG if ext == 'wav' and CONVERT_TYPE_CHK[convert_type] == 'ogg': logger.debug('WAV to OGG') conv = "sox %s %s.ogg" % (filename, splitted_filename) #conv = "ffmpeg -i %s -acodec libvorbis %s.ogg" % (filename, splitted_filename) result = audio_convert_task.delay(conv) logger.debug("command :> %s" % conv) # 5) OGG TO MP3 if ext == 'ogg' and CONVERT_TYPE_CHK[convert_type] == 'mp3': logger.debug('OGG to MP3') conv = "sox %s %s.mp3" % (filename, splitted_filename) #conv = "ffmpeg -i %s -acodec libmp3lame %s.mp3" % (filename, splitted_filename) result = audio_convert_task.delay(conv) logger.debug("command :> %s" % conv) # 6) OGG TO WAV if ext == 'ogg' and CONVERT_TYPE_CHK[convert_type] == 'wav': logger.debug('OGG to WAV') #conv = "sox %s %s.wav" % (filename, splitted_filename) conv = "ffmpeg -i %s %s.wav" % (filename, splitted_filename) result = audio_convert_task.delay(conv) logger.debug("command :> %s" % conv)
def _convert_audio(self, filename, instance=None, ext=None): '''Convert uploaded audio file to selected format''' request = threadlocals.get_current_request() convert_type = int(request.POST["convert_type"]) channel_no = int(request.POST["channel_type"]) freq_value = int(request.POST["freq_type"]) splitted_filename = list(os.path.splitext(filename))[0] # converted filename without ext logger.debug("convert audio : %s->%s" % (str(ext), CONVERT_TYPE_CHK[convert_type])) filename_temp = filename[:-4] + '_temp' + '.wav' # 1) MP3 TO WAV if ext == 'mp3' and CONVERT_TYPE_CHK[convert_type] == 'wav': logger.debug("convert MP3 to WAV - channel %s freq: %s" % (str(channel_no), str(freq_value))) #prepare Sox parameters for Channels convertion conv_channel = "-s -c %s" % str(channel_no) if channel_no > 0 else '' #prepare Sox parameters for Frequency convertion conv_freq = "-r %s" % str(freq_value) if freq_value > 0 else '' conv = "sox %s %s %s %s.wav" % (filename, conv_freq, conv_channel, splitted_filename) result = audio_convert_task.delay(conv) #commands.getoutput(conv) logger.debug("Sox command :> %s" % conv) #TODO: CONVERT MP3 TO OGG # 2) MP3 TO OGG if ext == 'mp3' and CONVERT_TYPE_CHK[convert_type] == 'ogg': logger.debug('MP3 to OGG') conv = "mpg321 %s -w raw && oggenc raw -o %s.ogg" % (filename, splitted_filename) #conv = "sox %s %s.ogg" % (filename, filename) #conv = "ffmpeg -i %s %s.ogg" % (filename, splitted_filename) result = audio_convert_task.delay(conv) #commands.getoutput(conv) logger.debug("command :> %s" % conv) # 3) WAV TO MP3 if ext == 'wav' and CONVERT_TYPE_CHK[convert_type] == 'mp3': logger.debug('WAV to MP3') #conv = "lame -V2 %s %s.mp3" % (filename, filename) #conv = "lame -h %s %s.mp3" % (filename, filename) conv = "sox %s %s.mp3" % (filename, splitted_filename) result = audio_convert_task.delay(conv) #commands.getoutput(conv) logger.debug("Sox command :> %s" % conv) # 3) WAV TO WAV if ext == 'wav' and CONVERT_TYPE_CHK[convert_type] == 'wav': logger.debug("convert WAV to WAV - channel %s freq: %s" % (str(channel_no), str(freq_value))) #prepare Sox parameters for Channels convertion conv_channel = "-s -c %s" % str(channel_no) if channel_no > 0 else '' #prepare Sox parameters for Frequency convertion conv_freq = "-r %s" % str(freq_value) if freq_value > 0 else '' conv = "sox %s %s %s %s.wav" % (filename_temp, conv_freq, conv_channel, splitted_filename) #create a temp copy of the file shutil.copy2(filename, filename_temp) subprocess.Popen(conv.split(' '), stdout=subprocess.PIPE).communicate()[0] logger.debug("Sox command :> %s" % conv) #remove file after convertion os.remove(filename_temp) # 4) WAV TO OGG # not working if ext == 'wav' and CONVERT_TYPE_CHK[convert_type] == 'ogg': logger.debug('WAV to OGG') #conv = "sox %s %s.ogg" % (filename, splitted_filename) conv = "ffmpeg -i %s -acodec libvorbis %s.ogg" % (filename, splitted_filename) result = audio_convert_task.delay(conv) #commands.getoutput(conv) logger.debug("command :> %s" % conv) # 5) OGG TO MP3 # not working if ext == 'ogg' and CONVERT_TYPE_CHK[convert_type] == 'mp3': logger.debug('OGG to MP3') #conv = "sox %s %s.mp3" % (filename, splitted_filename) conv = "ffmpeg -i %s -acodec libmp3lame %s.mp3" % (filename, splitted_filename) result = audio_convert_task.delay(conv) #commands.getoutput(conv) logger.debug("command :> %s" % conv) # 6) OGG TO WAV if ext == 'ogg' and CONVERT_TYPE_CHK[convert_type] == 'wav': logger.debug('OGG to WAV') #conv = "sox %s %s.wav" % (filename, splitted_filename) conv = "ffmpeg -i %s %s.wav" % (filename, splitted_filename) result = audio_convert_task.delay(conv) #commands.getoutput(conv) logger.debug("command :> %s" % conv)