Skip to content

Commit

Permalink
Fix cannot upload first file (#55)
Browse files Browse the repository at this point in the history
* [fix] - 初めてのアップロード時の容量計算ならテーブルを作って0を返す

* [fix] - if not existsのSQL文は成功パターンを全部返すらしい
  • Loading branch information
NfoAlex authored Jul 29, 2024
1 parent 8a1bc57 commit 8d9d35a
Showing 1 changed file with 99 additions and 48 deletions.
147 changes: 99 additions & 48 deletions src/util/FIle/calcDirectorySize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,112 @@ export default function calcDirectorySize(
//ルートディレクトリを計算するかどうかで処理を変える
if (optionCalculatingRootDirectory) {

db.all(
`
SELECT * FROM FILE` + userId + `
`
,
(err:Error, files:IFile[]) => {
if (err) {
console.log("calcDirectorySize :: db : エラー->", err);
resolve(null);
return;
} else {
console.log("calcDirectorySize :: db : 結果(filesの数)->", files.length);

//ループして容量を加算
for (let file of files) {
totalSize += file.size;
db.serialize(() => {
//この人用のファイルインデックス用テーブルが無ければここで作成
db.run(
`
create table if not exists FILE` + userId + `(
id TEXT PRIMARY KEY,
userId TEXT DEFAULT ` + userId + `,
name TEXT NOT NULL,
actualName TEXT NOT NULL,
isPublic BOOLEAN NOT NULL DEFAULT 0,
type TEXT NOT NULL,
size NUMBER NOT NULL,
directory TEXT NOT NULL,
uploadedDate TEXT NOT NULL
)
`,
(err:Error) => {
if (err) {
resolve(null);
return;
}

console.log("calcDirectorySize :: db : 結果->", totalSize);

resolve(totalSize);
return;
}
}
);
);

db.all(
`
SELECT * FROM FILE` + userId + `
`
,
(err:Error, files:IFile[]) => {
if (err) {
console.log("calcDirectorySize :: db : エラー->", err);
resolve(null);
return;
} else {
console.log("calcDirectorySize :: db : 結果(filesの数)->", files.length);

//ループして容量を加算
for (let file of files) {
totalSize += file.size;
}

console.log("calcDirectorySize :: db : 結果->", totalSize);

resolve(totalSize);
return;
}
}
);
});

} else {

db.all(
`
SELECT * FROM FILE` + userId + `
WHERE directory=?
`
,
[directoryId],
(err:Error, files:IFile[]) => {
if (err) {
console.log("calcDirectorySize :: db : エラー->", err);
resolve(null);
return;
} else {
console.log("calcDirectorySize :: db : 結果(filesの数)->", files.length);

//ループして容量を加算
for (let file of files) {
totalSize += file.size;
db.serialize(() => {
//この人用のファイルインデックス用テーブルが無ければここで作成
db.run(
`
create table if not exists FILE` + userId + `(
id TEXT PRIMARY KEY,
userId TEXT DEFAULT ` + userId + `,
name TEXT NOT NULL,
actualName TEXT NOT NULL,
isPublic BOOLEAN NOT NULL DEFAULT 0,
type TEXT NOT NULL,
size NUMBER NOT NULL,
directory TEXT NOT NULL,
uploadedDate TEXT NOT NULL
)
`,
(err:Error) => {
if (err) {
resolve(null);
return;
}
}
);

db.all(
`
SELECT * FROM FILE` + userId + `
WHERE directory=?
`
,
[directoryId],
(err:Error, files:IFile[]) => {
if (err) {
console.log("calcDirectorySize :: db : エラー->", err);
resolve(null);
return;
} else {
console.log("calcDirectorySize :: db : 結果(filesの数)->", files.length);

//ループして容量を加算
for (let file of files) {
totalSize += file.size;
}

console.log("calcDirectorySize :: db : 結果->", totalSize);

resolve(totalSize);
return;
}

console.log("calcDirectorySize :: db : 結果->", totalSize);

resolve(totalSize);
return;
}
}
);
);

});

}

Expand Down

0 comments on commit 8d9d35a

Please sign in to comment.