s3.js 16.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
// 사용자 폴더: /drive/userId/
// 휴지통 폴더: /drive/userId/trashcan

// /drive/userId/folder1/file.txt를 /drive/userId/trashcan/folder1/file.txt로 옮길 때
// (휴지통의 경우 폴더까지 휴지통으로 그대로 복사)
// sourceFile: folder1/file.txt
// targetPath: trashcan
// targetFile: trashcan/folder1/file.txt

// /drive/userId/folder1/file.txt를 /drive/userId/folder2/file.txt로 옮길 때
// sourceFile: folder1/file.txt
// targetPath: folder2
// targetFile: folder2/file.txt


var AWS = require('aws-sdk');
AWS.config.update({ region: 'ap-northeast-2' });

var BUCKET_NAME = 'khuloud';

var s3 = new AWS.S3();
var fs = require('fs');
var path = require('path');


var S3 = {
    BUCKET_NAME: BUCKET_NAME,

    copyFile: function (bucketName, userId, sourceFile, targetPath, callback) {
        if (targetPath != '') {
            targetPath = targetPath + '/';
        }

        var copyParams = {
            Bucket: bucketName,
            CopySource: bucketName + '/drive/' + userId + '/' + sourceFile,
            Key: 'drive/' + userId + '/' + targetPath + sourceFile       // drive/userId/trashcan/file.txt
        }

        s3.copyObject(copyParams, function (err, data) {
            if (err) {
                console.log("Copy Error", err);
                callback(false);
            } else {
                if (data) {
                    console.log("Copy Success");
                    callback(true);
                }
            }
        })
    },

    copyFile2: function (bucketName, userId, sourceFile, targetFile, callback) {
        // sourceFile => folder1/folder2/test.txt
        // targetFile => trashcan/folder1/folder2/test.txt
        var copyParams = {
            Bucket: bucketName,
            CopySource: bucketName + '/drive/' + userId + '/' + sourceFile,
            Key: 'drive/' + userId + '/' + targetFile
        }

        s3.copyObject(copyParams, function (err, data) {
            if (err) {
                console.log("Copy Error", err);
                callback(false);
            } else {
                if (data) {
                    console.log("Copy Success");
                    callback(true);
                }
            }
        })
    },

    // 파일 덮어쓰기
    coverFile: function(bucketName, userId, sourceFile, targetPath, body, callback) {
        var pathbody = fs.createReadStream(body);

        if (targetPath != '' && targetPath[targetPath.length - 1] != '/') {
            targetPath = targetPath + '/';
        }
        var targetFile = targetPath + sourceFile;

        var coverParams = {
            Bucket: bucketName,
            Key: 'drive/' + userId + '/' + targetFile,
            Body: pathbody
        };

        s3.upload(coverParams, function (err, data) {
            if (err) {
                console.log("Cover Error" + err);
                callback(false);
            } else {
                console.log("Cover Success");
                callback(true);
            }
        })
    },

    deleteFile: function (bucketName, userId, targetFile, callback) {
        // targetFile => folder1/folder2/test.txt
        var deleteParams = {
            Bucket: bucketName,
            Key: 'drive/' + userId + '/' + targetFile
        };

        s3.deleteObject(deleteParams, function (err, data) {
            if (err) {
                console.log("Delete Error", err);
                callback(false);
            } else {
                if (data) {
                    console.log("DeleteSuccess");
                    callback(true);
                }
            }
        })
    },

    downloadFile: function (bucketName, userId, targetFile, callback) {
        var downloadParams = {
            Bucket: bucketName,
            Key: 'drive/' + userId + '/' + targetFile
        };
        console.log('drive/' + userId + '/' + targetFile);
        s3.getObject(downloadParams, function (err, data) {
            if (err) {
                console.log("Download File Error", err);
                callback(false);
            } else {
                console.log("Get File Success");
                callback(true, data.Body.toString());
            }
        })
    },

    // /routes/modules/s3/download에 저장
    downloadFile2: function(bucketName, userId, targetFile, callback){
        tempDownloadDir = __dirname + '/download/' + userId + '/' + targetFile;
        S3.downloadFile(bucketName, userId, targetFile, function(result, data){
            if (result) {
                makeFolder(tempDownloadDir, function(result){
                    if (result) {
                        fs.writeFileSync(tempDownloadDir, data);
                        callback(true, tempDownloadDir);
                    }
                })
            }else{
                console.log('Download File Error');
                callback(false);
            }
        })
    },

    // 최종
    downloadFile3: function(bucketName, userId, targetFile, callback){
        // targetFile 예1 => test.txt
        // targetFile 예2 => folder1/folder2/test.txt
        var tempDownloadDir = __dirname + '/download/' + userId + '/' + targetFile;
        makeFolder(tempDownloadDir, function(result){
            if (result){
                var file = fs.createWriteStream(tempDownloadDir);
                var params = {
                    Bucket: bucketName,
                    Key: 'drive/' + userId + '/' + targetFile
                };
                try {
                    var stream = s3.getObject(params).createReadStream().pipe(file);
                    stream.on('end', function(){
                        console.log('end!');
                        callback(true, tempDownloadDir);
                    });
                }catch(err){
                    console.log('no such file', err);
                    callback(false);
                }
            }else{
                callback(false);
            }
        })
    },

    getFileList: function (bucketName, userId, targetPath, callback) {
        var prefix;

        if (targetPath == '') {
            prefix = 'drive/' + userId + '/';
        } else {
            prefix = 'drive/' + userId + '/' + targetPath + '/';
        }

        var bucketParams = {
            Bucket: bucketName,
            Prefix: prefix
        };

        s3.listObjects(bucketParams, function (err, data) {
            if (err) {
                console.log("getList Error", err);
                callback(false, data);
            } else {
                if (data) {
                    console.log("getList Success");
                    console.log("data : ", data);
                    callback(true, data);
                }
            }
        })
    },

    isFileOverlapped: function (bucketName, userId, targetFile, callback) {
        var paths = targetFile.split('/');
        var index = targetFile.length - (paths[paths.length - 1].length + 1);
        var targetPath = targetFile.substring(0, index);

        S3.getFileList(bucketName, userId, targetPath, function (res, data) {
            var answer = false;
            var lvNum;

            if (!res) {
                console.log("Overlap Check Error on Get List");
                callback(false, answer, lvNum);
            } else {
                if (data) {
                    for (var i = 0; i < data.Contents.length; i++) {
                        var fullpath = data.Contents[i].Key;
                        var paths = fullpath.split('/');
                        var index = paths[0].length + paths[1].length + 2;
                        if (fullpath.substring(index) == targetFile) {
                            answer = true;
                            var splited = targetFile.split('(').join(',').split(')').join(',').split(',');
                            if (splited.length != 3){
                                lvNum = 0;
                            }else{
                                lvNum = parseInt(splited[1]);
                                console.log('isNum', lvNum);
                            }
                            break;
                        }
                    }
                    console.log("Overlap Check Success");
                    callback(true, answer, lvNum);
                }
            }
        })
    },

    // 중복된 파일 버전 만들기(test.txt -> text(1).txt)
    makeVersion: function (bucketName, userId, sourceFile, lvNum, callback) {
        var sourceFile;
        var splited = sourceFile.split('(').join(',').split(')').join(',').split(',');
        if (splited.length != 3) {
            sourceFile = sourceFile.split('.')[0] + '(' + lvNum.toString() + ').' + sourceFile.split('.')[1];
        } else {
            sourceFile = sourceFile.split('(')[0] + '(' + lvNum.toString() + ')' + sourceFile.split(')')[1];
        }
        console.log('makeVersion ', sourceFile);

        callback(true, sourceFile);
    },

    // sourceFile이 단순히 filename과 같은 경우
    moveFile: function (bucketName, userId, sourceFile, targetPath, callback) {
        S3.copyFile(bucketName, userId, sourceFile, targetPath, function (res) {
            if (!res) {
                console.log("Move Error on Copying File");
                callback(false);
            } else {
                S3.deleteFile(bucketName, userId, sourceFile, function (res) {
                    if (!res) {
                        console.log("Move Error on Deleting File");
                        callback(false);
                    } else {
                        console.log("Move Success");
                        callback(true);
                    }
                })
            }
        })
    },

    // sourceFile에 임의의 경로가 포함된 경우
    moveFile2: function (bucketName, userId, sourceFile, targetPath, callback) {
        // sourceFile => folder1/folder2/test.txt
        var targetFile = targetPath + '/' + sourceFile;

        S3.copyFile2(bucketName, userId, sourceFile, targetFile, function (res) {
            if (!res) {
                console.log("Move Error on Copying File2");
                callback(false);
            } else {
                S3.deleteFile(bucketName, userId, sourceFile, function (res) {
                    if (!res) {
                        console.log("Move Error on Deleting File");
                        callback(false);
                    } else {
                        console.log("Move Success");
                        callback(true);
                    }
                })
            }
        })
    },

    // 파일 이름이 달라지는 경우
    moveFile3: function (bucketName, userId, oldFile, newFile, targetPath, callback) {
        if (targetPath != ''){
            targetPath = targetPath + '/';
        }
        sourceFile = targetPath + oldFile;      // test.txt or folder1/test.txt
        var targetFile = targetPath + newFile;

        S3.copyFile2(bucketName, userId, sourceFile, targetFile, function(result){
            if (!result){
                console.log("Move Error on Copying File3");
                callback(false);
            }else{
                S3.deleteFile(bucketName, userId, sourceFile, function (res) {
                    if (!res) {
                        console.log("Move Error on Deleting File");
                        callback(false);
                    } else {
                        console.log("Move Success");
                        callback(true);
                    }
                })
            }
        })
    }, 

    renameFile: function(bucketName, userId, sourceFile, modiFile, targetPath, callback){
        // sourceFile = test.txt
        // modiFile = test2.txt
        // targetPath = '' or 'folder1/folder2'

        if (sourceFile == modiFile){    // 이름 변경되지 않은 경우
            callback(true, sourceFile);
        }else{
            if (targetPath != '') {
                targetPath = targetpath + '/';
            }
            var targetFile = targetPath + modiFile;

            S3.isFileOverlapped(bucketName, userId, targetFile, function (res, ans, lvNum) {
                if (!res) {
                    console.log("Overlap Check failed");
                    callback(false, sourceFile);
                } else {
                    if (ans) {
                        console.log("File Duplication");
                        S3.makeVersion(bucketName, userId, targetFile, lvNum + 1, function (res, versionedSourceFile) {
                            if (!res) {
                                console.log("Make version failed");
                                callback(false, sourceFile);
                            } else {
                                S3.renameFile(bucketName, userId, sourceFile, versionedSourceFile, targetPath, callback);
                            }
                        })
                    } else {
                        S3.moveFile3(bucketName, userId, sourceFile, modiFile, targetPath, function (result) {
                            if (result) {
                                callback(true, modiFile);
                            } else {
                                console.log('File Rename failed');
                                callback(false);
                            }
                        })
                    }
                }
            });
        }
    },

    uploadFile: function (bucketName, userId, sourceFile, targetPath, body, callback) {
        var pathbody = fs.createReadStream(body);

        if (targetPath!='' && targetPath[targetPath.length-1]!='/') {
            targetPath = targetPath + '/';
        }
        var targetFile = targetPath + sourceFile;
        console.log('targetFile', targetFile);

        var uploadParams = {
            Bucket: bucketName,
            Key: 'drive/' + userId + '/' + targetFile,
            Body: pathbody
        };
        S3.isFileOverlapped(bucketName, userId, targetFile, function (res, ans, lvNum) {
            if (!res) {
                console.log("Overlap Check failed");
                callback(false, sourceFile);
            } else {
                if (ans) {
                    console.log("File Duplication");
                    S3.makeVersion(bucketName, userId, sourceFile, lvNum+1, function(res, versionedSourceFile){
                        if (!res){
                            console.log("Make version failed");
                            callback(false, sourceFile);
                        }else{
                            S3.uploadFile(bucketName, userId, versionedSourceFile, targetPath, body, callback);
                        }
                    })
                } else {
                    s3.upload(uploadParams, function (err, data) {
                        if (err) {
                            console.log("Upload Error" + err);
                            callback(false, sourceFile);
                        } else {
                            console.log("Upload Success");
                            callback(true, sourceFile);
                        }
                    })
                }
            }
        })

    },

    uploadFiles: function (iter, errFiles, bucketName, userId, sourceFiles, targetPath, bodies, callback) {
        if (iter < sourceFiles.length) {
            S3.uploadFile(bucketName, userId, sourceFiles[iter], targetPath, bodies[iter], function (res, resSourceFile) {
                if (!res) {
                    errFiles.push(sourceFiles[iter]);
                }
                if (sourceFiles[iter] != resSourceFile){    // 파일 중복으로 다른 버전이 생긴 경우
                    sourceFiles[iter] = resSourceFile;
                }
                S3.uploadFiles(iter + 1, errFiles, bucketName, userId, sourceFiles, targetPath, bodies, callback);
            })
        } else {
            if (errFiles.length != 0) {
                console.log("Upload Files Error on Uploading Some Files");
                callback(false, errFiles);
            } else {
                console.log("Upload Files Success");
                callback(true, errFiles);
            }
        }
    },
}

var makeFolder = function(dir, callback){
    var paths = dir.substring(__dirname.length+1);     // download/userId/folder1/folder2/test.txt
    paths = paths.split('/');

    var folders = __dirname;
    for(var i=0; i<paths.length-1; i++){
        folders += '/'+paths[i];
        if(!fs.existsSync(folders)){
            fs.mkdirSync(folders)
        }
    }

    if (fs.existsSync(folders)){
        callback(true);
    }else{
        callback(false);
    }
}

module.exports = S3;