Store all temp files in app named temp subdirectory

This commit is contained in:
Rafał Dzięgiel
2021-03-17 11:57:02 +01:00
parent 4021745a56
commit fcff4b4450
2 changed files with 18 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
const { Gio, GLib } = imports.gi; const { Gio, GLib } = imports.gi;
const Debug = imports.src.debug; const Debug = imports.src.debug;
const FileOps = imports.src.fileOps;
const Misc = imports.src.misc; const Misc = imports.src.misc;
const { debug } = Debug; const { debug } = Debug;
@@ -45,11 +46,14 @@ function generateDash(info)
function saveDashPromise(dash) function saveDashPromise(dash)
{ {
return new Promise((resolve, reject) => { debug('saving dash file');
const tempPath = GLib.get_tmp_dir() + '/.clapper.mpd';
const dashFile = Gio.File.new_for_path(tempPath);
debug('saving dash file'); return new Promise(async (resolve, reject) => {
const tempDir = await FileOps.createTempDirPromise().catch(debug);
if(!tempDir)
return reject(new Error('could not create folder in temp directory'));
const dashFile = tempDir.get_child('.clapper.mpd');
dashFile.replace_contents_bytes_async( dashFile.replace_contents_bytes_async(
GLib.Bytes.new_take(dash), GLib.Bytes.new_take(dash),

View File

@@ -18,7 +18,7 @@ function createCacheDirPromise()
function createTempDirPromise() function createTempDirPromise()
{ {
const dir = Gio.File.new_for_path( const dir = Gio.File.new_for_path(
GLib.get_tmp_dir() + '/.' + Misc.appId GLib.get_tmp_dir() + '/' + Misc.appId
); );
return createDirPromise(dir); return createDirPromise(dir);
@@ -26,18 +26,20 @@ function createTempDirPromise()
function createDirPromise(dir) function createDirPromise(dir)
{ {
return new Promise(async (resolve, reject) => { return new Promise((resolve, reject) => {
if(dir.query_exists(null)) if(dir.query_exists(null))
return resolve(dir); return resolve(dir);
const dirCreated = await dir.make_directory_async( dir.make_directory_async(
GLib.PRIORITY_DEFAULT, GLib.PRIORITY_DEFAULT,
null, null,
).catch(debug); )
.then(success => {
if(success)
return resolve(dir);
if(!dirCreated) reject(new Error(`could not create dir: ${dir.get_path()}`));
return reject(new Error(`could not create dir: ${dir.get_path()}`)); })
.catch(err => reject(err));
resolve(dir);
}); });
} }