1
1
import { NextFunction , Request , Response } from "express" ;
2
2
import conductorErrors from "../conductor-errors.js" ;
3
- import ProjectFile , { ProjectFileInterface } from "../models/projectfile.js" ;
3
+ import ProjectFile , {
4
+ ProjectFileInterface ,
5
+ RawProjectFileInterface ,
6
+ } from "../models/projectfile.js" ;
4
7
import multer from "multer" ;
5
8
import Project from "../models/project.js" ;
6
9
import {
@@ -532,13 +535,6 @@ async function updateProjectFile(
532
535
) {
533
536
try {
534
537
const { projectID, fileID } = req . params ;
535
- const project = await Project . findOne ( { projectID } ) . lean ( ) ;
536
- if ( ! project ) {
537
- return res . status ( 404 ) . send ( {
538
- err : true ,
539
- errMsg : conductorErrors . err11 ,
540
- } ) ;
541
- }
542
538
543
539
const {
544
540
name,
@@ -555,18 +551,14 @@ async function updateProjectFile(
555
551
const shouldOverwriteName =
556
552
overwriteName === undefined ? true : overwriteName ;
557
553
558
- const files = await retrieveAllProjectFiles (
554
+ const [ file ] = await retrieveSingleProjectFile (
559
555
projectID ,
556
+ fileID ,
560
557
false ,
561
558
req . user . decoded . uuid
562
559
) ;
563
- if ( ! files ) {
564
- // error encountered
565
- throw new Error ( "retrieveerror" ) ;
566
- }
567
560
568
- const foundObj = files . find ( ( obj ) => obj . fileID === fileID ) ;
569
- if ( ! foundObj ) {
561
+ if ( ! file ) {
570
562
return res . status ( 400 ) . send ( {
571
563
err : true ,
572
564
errMsg : conductorErrors . err63 ,
@@ -589,7 +581,7 @@ async function updateProjectFile(
589
581
if ( processedName ) {
590
582
// Ensure file extension remains in new name
591
583
if ( ! processedName . includes ( "." ) ) {
592
- const splitCurrName = foundObj . name ?. split ( "." ) ?? [ ] ;
584
+ const splitCurrName = file . name ?. split ( "." ) ?? [ ] ;
593
585
if ( splitCurrName . length > 1 ) {
594
586
const currExtension = splitCurrName [ splitCurrName . length - 1 ] ;
595
587
processedName = `${ processedName } .${ currExtension } ` ;
@@ -600,79 +592,73 @@ async function updateProjectFile(
600
592
// update tags
601
593
if ( tags ) {
602
594
//@ts -ignore
603
- await upsertAssetTags ( foundObj , tags ) ;
595
+ await upsertAssetTags ( file , tags ) ;
604
596
}
605
597
606
- const updated = files . map ( ( obj ) => {
607
- if ( obj . fileID === foundObj . fileID ) {
608
- const updateObj = { ...obj } ;
609
- if ( processedName ) {
610
- updateObj . name = processedName ;
611
- }
612
- if ( typeof description === "string" ) {
613
- // account for unsetting
614
- updateObj . description = description ;
615
- }
616
- if ( license ) {
617
- updateObj . license = license ;
618
- }
619
- if ( authors ) {
620
- const parseAuthors = ( authorsData : any [ ] ) => {
621
- if ( ! Array . isArray ( authorsData ) ) return [ ] ;
622
- const reduced = authorsData . reduce ( ( acc , curr ) => {
623
- if ( curr . _id ) {
624
- acc . push ( new Types . ObjectId ( curr . _id ) ) ;
625
- } else {
626
- acc . push ( curr ) ;
627
- }
628
- return acc ;
629
- } , [ ] ) ;
630
- return reduced ;
631
- } ;
598
+ const updateObj = { } as RawProjectFileInterface ;
599
+ if ( processedName ) {
600
+ updateObj . name = processedName ;
601
+ }
602
+ if ( typeof description === "string" ) {
603
+ // account for unsetting
604
+ updateObj . description = description ;
605
+ }
606
+ if ( license ) {
607
+ updateObj . license = license ;
608
+ }
609
+ if ( authors ) {
610
+ const parseAuthors = ( authorsData : any [ ] ) => {
611
+ if ( ! Array . isArray ( authorsData ) ) return [ ] ;
612
+ const reduced = authorsData . reduce ( ( acc , curr ) => {
613
+ if ( curr . _id ) {
614
+ acc . push ( new Types . ObjectId ( curr . _id ) ) ;
615
+ } else {
616
+ acc . push ( curr ) ;
617
+ }
618
+ return acc ;
619
+ } , [ ] ) ;
620
+ return reduced ;
621
+ } ;
632
622
633
- const parsed = parseAuthors ( authors ) ;
623
+ const parsed = parseAuthors ( authors ) ;
634
624
635
- updateObj . authors = parsed ;
636
- }
637
- if ( publisher ) {
638
- updateObj . publisher = publisher ;
639
- }
640
- if ( req . files && req . files [ 0 ] ) {
641
- updateObj . version = obj . version ? obj . version + 1 : 1 ; // increment version
642
- if ( req . files [ 0 ] . mimetype ) {
643
- updateObj . mimeType = req . files [ 0 ] . mimetype ; // update mime type
644
- }
645
- if ( req . files [ 0 ] . size ) {
646
- updateObj . size = req . files [ 0 ] . size ; // update size
647
- }
648
- }
649
- // allow updating of URL if file is a URL
650
- if (
651
- Boolean ( isURL ) &&
652
- fileURL
653
- //&& obj.isURL && obj.url !== fileURL
654
- ) {
655
- updateObj . isURL = true ;
656
- updateObj . url = fileURL ;
657
- updateObj . storageType = "file" ;
658
- updateObj . size = 0 ;
659
- updateObj . downloadCount = undefined ;
660
- updateObj . mimeType = undefined ;
661
- updateObj . license = {
662
- ...obj . license ,
663
- sourceURL : fileURL ,
664
- } ;
665
- }
666
- return updateObj ;
625
+ updateObj . authors = parsed ;
626
+ }
627
+ if ( publisher ) {
628
+ updateObj . publisher = publisher ;
629
+ }
630
+ if ( req . files && req . files [ 0 ] ) {
631
+ updateObj . version = file . version ? file . version + 1 : 1 ; // increment version
632
+ if ( req . files [ 0 ] . mimetype ) {
633
+ updateObj . mimeType = req . files [ 0 ] . mimetype ; // update mime type
667
634
}
668
- return obj ;
669
- } ) ;
635
+ if ( req . files [ 0 ] . size ) {
636
+ updateObj . size = req . files [ 0 ] . size ; // update size
637
+ }
638
+ }
639
+ // allow updating of URL if file is a URL
640
+ if (
641
+ Boolean ( isURL ) &&
642
+ fileURL
643
+ //&& obj.isURL && obj.url !== fileURL
644
+ ) {
645
+ updateObj . isURL = true ;
646
+ updateObj . url = fileURL ;
647
+ updateObj . storageType = "file" ;
648
+ updateObj . size = 0 ;
649
+ updateObj . downloadCount = undefined ;
650
+ updateObj . mimeType = undefined ;
651
+ updateObj . license = {
652
+ ...file . license ,
653
+ sourceURL : fileURL ,
654
+ } ;
655
+ }
670
656
671
657
const storageClient = new S3Client ( PROJECT_FILES_S3_CLIENT_CONFIG ) ;
672
658
673
659
const isPhysicalFile =
674
- foundObj . storageType === "file" && ! foundObj . isURL && ! foundObj . url ;
675
- if ( isPhysicalFile && processedName && processedName !== foundObj . name ) {
660
+ file . storageType === "file" && ! file . isURL && ! file . url ;
661
+ if ( isPhysicalFile && processedName && processedName !== file . name ) {
676
662
// rename file
677
663
const fileKey = `${ projectID } /${ fileID } ` ;
678
664
const storageClient = new S3Client ( PROJECT_FILES_S3_CLIENT_CONFIG ) ;
@@ -717,7 +703,7 @@ async function updateProjectFile(
717
703
}
718
704
719
705
// Delete the old file if it has been replaced with a URL
720
- if ( foundObj . storageType === "file" && isURL && fileURL ) {
706
+ if ( file . storageType === "file" && isURL && fileURL ) {
721
707
await storageClient . send (
722
708
new DeleteObjectCommand ( {
723
709
Bucket : process . env . AWS_PROJECTFILES_BUCKET ,
@@ -726,10 +712,13 @@ async function updateProjectFile(
726
712
) ;
727
713
}
728
714
729
- const projectUpdate = await updateProjectFilesUtil ( projectID , updated ) ;
730
- if ( ! projectUpdate ) {
731
- throw new Error ( "updatefail" ) ;
732
- }
715
+ await ProjectFile . findOneAndUpdate (
716
+ {
717
+ projectID,
718
+ fileID,
719
+ } ,
720
+ updateObj
721
+ ) ;
733
722
734
723
return res . send ( {
735
724
err : false ,
@@ -956,12 +945,12 @@ async function removeProjectFile(
956
945
} ) . lean ( ) ;
957
946
958
947
objsToDelete . push ( found ) ;
959
- if ( children . length > 0 ) {
960
- objsToDelete . push ( ...children ) ;
948
+ if ( children . length > 0 ) {
949
+ objsToDelete . push ( ...children ) ;
961
950
}
962
951
963
952
const objectIDs = objsToDelete . map ( ( obj ) => obj . fileID ) ;
964
-
953
+
965
954
const filesToDelete = objsToDelete
966
955
. map ( ( obj ) => {
967
956
if ( obj . storageType === "file" ) {
0 commit comments