You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.6 KiB
80 lines
2.6 KiB
11 years ago
|
from couchpotato import get_db
|
||
14 years ago
|
from couchpotato.api import addApiView
|
||
14 years ago
|
from couchpotato.core.event import addEvent
|
||
14 years ago
|
from couchpotato.core.helpers.encoding import toUnicode
|
||
14 years ago
|
from couchpotato.core.helpers.variable import md5, getExt
|
||
|
from couchpotato.core.logger import CPLog
|
||
|
from couchpotato.core.plugins.base import Plugin
|
||
|
from couchpotato.environment import Env
|
||
11 years ago
|
from scandir import scandir
|
||
12 years ago
|
from tornado.web import StaticFileHandler
|
||
14 years ago
|
import os.path
|
||
13 years ago
|
import time
|
||
13 years ago
|
import traceback
|
||
14 years ago
|
|
||
|
log = CPLog(__name__)
|
||
|
|
||
11 years ago
|
autoload = 'FileManager'
|
||
|
|
||
14 years ago
|
|
||
|
class FileManager(Plugin):
|
||
|
|
||
|
def __init__(self):
|
||
|
addEvent('file.download', self.download)
|
||
|
|
||
12 years ago
|
addApiView('file.cache/(.*)', self.showCacheFile, static = True, docs = {
|
||
13 years ago
|
'desc': 'Return a file from the cp_data/cache directory',
|
||
|
'params': {
|
||
|
'filename': {'desc': 'path/filename of the wanted file'}
|
||
|
},
|
||
|
'return': {'type': 'file'}
|
||
|
})
|
||
14 years ago
|
|
||
11 years ago
|
addEvent('app.load', self.cleanup)
|
||
|
|
||
13 years ago
|
def cleanup(self):
|
||
|
|
||
|
# Wait a bit after starting before cleanup
|
||
11 years ago
|
time.sleep(2)
|
||
13 years ago
|
log.debug('Cleaning up unused files')
|
||
|
|
||
|
try:
|
||
11 years ago
|
db = get_db()
|
||
|
cache_dir = Env.get('cache_dir')
|
||
|
medias = db.all('media', with_doc = True)
|
||
|
|
||
|
files = []
|
||
|
for media in medias:
|
||
|
file_dict = media['doc'].get('files', {})
|
||
|
for x in file_dict.keys():
|
||
|
files.extend(file_dict[x])
|
||
|
|
||
11 years ago
|
for f in scandir.scandir(cache_dir):
|
||
|
if os.path.splitext(f.name)[1] in ['.png', '.jpg', '.jpeg']:
|
||
|
file_path = os.path.join(cache_dir, f.name)
|
||
11 years ago
|
if toUnicode(file_path) not in files:
|
||
|
os.remove(file_path)
|
||
13 years ago
|
except:
|
||
|
log.error('Failed removing unused file: %s', traceback.format_exc())
|
||
|
|
||
12 years ago
|
def showCacheFile(self, route, **kwargs):
|
||
12 years ago
|
Env.get('app').add_handlers(".*$", [('%s%s' % (Env.get('api_base'), route), StaticFileHandler, {'path': Env.get('cache_dir')})])
|
||
14 years ago
|
|
||
12 years ago
|
def download(self, url = '', dest = None, overwrite = False, urlopen_kwargs = None):
|
||
|
if not urlopen_kwargs: urlopen_kwargs = {}
|
||
14 years ago
|
|
||
13 years ago
|
if not dest: # to Cache
|
||
|
dest = os.path.join(Env.get('cache_dir'), '%s.%s' % (md5(url), getExt(url)))
|
||
|
|
||
|
if not overwrite and os.path.isfile(dest):
|
||
|
return dest
|
||
|
|
||
14 years ago
|
try:
|
||
11 years ago
|
filedata = self.urlopen(url, **urlopen_kwargs)
|
||
14 years ago
|
except:
|
||
13 years ago
|
log.error('Failed downloading file %s: %s', (url, traceback.format_exc()))
|
||
14 years ago
|
return False
|
||
14 years ago
|
|
||
13 years ago
|
self.createFile(dest, filedata, binary = True)
|
||
14 years ago
|
return dest
|