Skip to content

Commit 5937067

Browse files
committed
fix: consolidate gives error if destination folder already has songs in library
fixes: #65
1 parent 20039c7 commit 5937067

4 files changed

Lines changed: 93 additions & 58 deletions

File tree

src/app/components/error-message.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
</div>
55
</template>
66

7+
<style lang="scss" scoped>
8+
.message {
9+
margin-top: 1.5rem;
10+
}
11+
</style>
12+
713
<script lang="ts">
814
import { Component, Prop, Vue } from 'vue-property-decorator';
915

src/app/scss/app.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
@import './buttons';
55
@import './layout';
66
@import './messages';
7+
@import './modal';
78
@import './scrollbars';
89
@import './tables';
910
@import './tags';

src/app/scss/modal.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.modal {
2+
.modal-background {
3+
background-color: hsla(0, 0%, 4%, 0.86);
4+
}
5+
6+
.modal-close {
7+
display: none;
8+
}
9+
}

src/app/views/consolidate-command.vue

Lines changed: 77 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,46 @@
4242
>
4343
consolidate
4444
</b-button>
45-
46-
<div v-if="didConsolidate" class="block mt-6">
47-
{{ didCopy ? 'Copied' : 'Moved' }} {{ numConsolidated }} /
48-
{{ numTracks }} tracks to
49-
<a @click="openTargetFolder()" class="has-text-link">
50-
{{ targetFolder }}
51-
</a>
52-
</div>
53-
54-
<p v-if="numMissing">
55-
<span class="has-text-warning">
56-
{{ numMissing }} tracks were not found
57-
</span>
58-
<br />
59-
Use
60-
<a @click="$router.push('relocate')" class="link-text">relocate</a>
61-
to fix this
62-
</p>
6345
</div>
6446

6547
<error-message :message="error"></error-message>
48+
49+
<b-modal v-model="didConsolidate" has-modal-card>
50+
<div class="modal-card card is-align-items-center py-6">
51+
<div class="block">
52+
{{ didCopy ? 'Copied' : 'Moved' }} {{ numConsolidated }} /
53+
{{ numTracks }} tracks to
54+
<a @click="openTargetFolder()" class="link-text">
55+
{{ targetFolder }}
56+
</a>
57+
</div>
58+
59+
<div v-if="numAlreadyConsolidated" class="block">
60+
<span class="has-text-info">
61+
{{ numAlreadyConsolidated }} tracks were already in the target
62+
folder
63+
</span>
64+
</div>
65+
66+
<div v-if="numMissing">
67+
<span class="has-text-warning">
68+
{{ numMissing }} tracks were not found
69+
</span>
70+
<br />
71+
Use
72+
<a @click="$router.push('relocate')" class="link-text">relocate</a>
73+
to fix this
74+
</div>
75+
76+
<b-button
77+
@click="didConsolidate = false"
78+
type="is-primary"
79+
class="mt-6"
80+
>
81+
ok
82+
</b-button>
83+
</div>
84+
</b-modal>
6685
</div>
6786
</template>
6887

@@ -89,6 +108,7 @@ interface ConsolidatableTrack {
89108
track: engine.Track;
90109
wasConsolidated: boolean;
91110
oldPath: string;
111+
newPath: string;
92112
}
93113
94114
@Component({
@@ -103,8 +123,10 @@ export default class ConsolidateCommand extends BaseCommand {
103123
104124
targetFolder = '';
105125
106-
allTracks: engine.Track[] | null = null;
107-
foundTracks: ConsolidatableTrack[] | null = null;
126+
allTracks: engine.Track[] = [];
127+
foundTracks: ConsolidatableTrack[] = [];
128+
alreadyConsolidatedTracks: engine.Track[] = [];
129+
missingTracks: engine.Track[] = [];
108130
109131
error = '';
110132
@@ -116,23 +138,24 @@ export default class ConsolidateCommand extends BaseCommand {
116138
return this.isConsolidating;
117139
}
118140
119-
get numTracks(): number | undefined {
120-
return this.allTracks?.length;
141+
get numTracks(): number {
142+
return this.allTracks.length;
121143
}
122144
123-
get consolidatedTracks(): ConsolidatableTrack[] | undefined {
124-
return this.foundTracks?.filter(x => x.wasConsolidated);
145+
get consolidatedTracks(): ConsolidatableTrack[] {
146+
return this.foundTracks.filter(x => x.wasConsolidated);
125147
}
126148
127-
get numConsolidated(): number | undefined {
128-
return this.consolidatedTracks?.length;
149+
get numConsolidated(): number {
150+
return this.consolidatedTracks.length;
129151
}
130152
131-
get numMissing(): number | undefined {
132-
if (!this.didConsolidate) {
133-
return 0;
134-
}
135-
return this.allTracks!.length - this.foundTracks!.length;
153+
get numAlreadyConsolidated(): number {
154+
return this.alreadyConsolidatedTracks.length;
155+
}
156+
157+
get numMissing(): number {
158+
return this.missingTracks.length;
136159
}
137160
138161
async consolidateLibrary() {
@@ -169,55 +192,51 @@ export default class ConsolidateCommand extends BaseCommand {
169192
170193
private async consolidateLibraryInternal() {
171194
const findTracks = async () => {
172-
const tracks = await this.engineDb!.getTracks();
173-
const found: ConsolidatableTrack[] = [];
195+
this.allTracks = await this.engineDb!.getTracks();
196+
this.foundTracks = [];
197+
this.alreadyConsolidatedTracks = [];
198+
this.missingTracks = [];
174199
175-
for (const track of tracks) {
200+
for (const track of this.allTracks) {
176201
const resolvedPath = path.resolve(this.libraryFolder, track.path);
177-
if (await checkPathExists(resolvedPath)) {
178-
found.push({
202+
const newPath = path.resolve(this.targetFolder, track.filename);
203+
204+
if (resolvedPath === newPath) {
205+
this.alreadyConsolidatedTracks.push(track);
206+
} else if (!(await checkPathExists(resolvedPath))) {
207+
this.missingTracks.push(track);
208+
} else {
209+
this.foundTracks.push({
179210
track,
180211
wasConsolidated: false,
181212
oldPath: resolvedPath,
213+
newPath,
182214
});
183215
}
184216
}
185-
186-
return { tracks, found };
187217
};
188218
189-
async function processFile(from: string, to: string, copy?: boolean) {
219+
const processFile = async (from: string, to: string, copy?: boolean) => {
190220
if (copy) {
191221
await fse.copyFile(from, to);
192222
} else {
193223
await fse.move(from, to);
194224
}
195-
}
225+
};
226+
227+
await findTracks();
196228
197-
const { tracks, found } = await findTracks();
198-
this.allTracks = tracks;
199-
this.foundTracks = found;
200-
201-
for (const item of found) {
202-
const targetPath = path.resolve(this.targetFolder, item.track.filename);
203-
item.track.path = makePathUnix(
204-
path.relative(this.libraryFolder, targetPath),
205-
);
206-
// no overwrite
207-
// if (path.normalize(item.oldPath) === path.normalize(item.track.path)) {
208-
// continue;
209-
// }
210-
211-
await processFile(item.oldPath, targetPath, this.shouldCopy);
229+
for (const item of this.foundTracks) {
230+
await processFile(item.oldPath, item.newPath, this.shouldCopy);
212231
item.wasConsolidated = true;
213232
}
214233
215234
await this.engineDb!.updateTracks(
216-
found
235+
this.foundTracks
217236
.filter(x => x.wasConsolidated)
218-
.map(({ track }) => ({
237+
.map(({ track, newPath }) => ({
219238
id: track.id,
220-
path: track.path,
239+
path: makePathUnix(path.relative(this.libraryFolder, newPath)),
221240
})),
222241
);
223242
}

0 commit comments

Comments
 (0)