<commit_before>spacialistApp.service('fileService', ['$rootScope', 'httpPostFactory', 'httpGetFactory', 'httpGetPromise', 'httpPutFactory', 'httpPatchFactory', 'httpDeleteFactory', 'snackbarService', 'searchService', 'Upload', '$translate', '$timeout', function($rootScope, httpPostFactory, httpGetFactory, httpGetPromise, httpPutFactory, httpPatchFactory, httpDeleteFactory, snackbarService, searchService, Upload, $translate, $timeout) {
    var images = {
        all: [],
        linked: [],
        upload: {}
    };

    images.tags = {
        visible: false
    };

    var lastTimeImageChecked = 0;

    images.getMimeType = function(f) {
        // check for extension before mime-type check
        var filename = f.filename;
        var mt = f.mime_type;
        if(filename) {
            var suffix = filename.substr(filename.lastIndexOf('.')+1);
            switch(suffix) {
                case 'dae':
                case 'obj':
                    return '3d';
            }
        }

        if(mt.startsWith('image/')) return 'image';
        if(mt == 'application/pdf') return 'pdf';
        if(mt == 'application/xml' || mt == 'text/xml') return 'xml';
        if(mt == 'application/xhtml+xml' || mt == 'text/html') return 'html';
        if(mt.startsWith('audio/')) return 'audio';
        if(mt.startsWith('video/')) return 'video';

        // default is simple text
        return 'text';
    };

    images.getImagesForContext = function(id) {
        if(!id) return;
        $rootScope.$emit('image:delete:linked');
        images.linked = [];
        images.getLinkedImages(id);
    };

    images.getLinkedImages = function(id) {
        if(!id) return;
        images.linked = filterLinkedImages(id);
    };

    images.loadImages = function(len, type) {
        var src = images[type];
        if(len == src.length) return src;
        var loaded = src.slice(0, len + 10);
        return loaded;
    };

    images.hasMoreImages = function(len, type) {
        var src = images[type];
        return src.length - len;
    };

    /**
     * Upload the image files `files` to the server, one by one and store their paths in the database.
     */
    images.uploadImages = function(files, invalidFiles, fileList) {
        var upload = {};
        upload.files = files;
        upload.errFiles = invalidFiles;
        upload.finished = 0;
        upload.toFinish = (typeof files === 'undefined') ? 0 : files.length;
        angular.forEach(files, function(file) {
            file.upload = Upload.upload({
                url: 'api/image/upload',
                data: { file: file }
            });
            file.upload.then(function(response) {
                $timeout(function() {
                    var data = response.data;
                    data.linked = [];
                    fileList.push(data);
                    upload.finished++;
                    if (upload.finished == upload.toFinish) {
                        $timeout(function() {
                            upload.files.length = 0;
                        }, 1000);
                        var content = $translate.instant('snackbar.image-upload.success', {
                            cnt: upload.finished
                        });
                        snackbarService.addAutocloseSnack(content, 'success');
                    }
                });
            }, function(response) {
                if(response.status > 0) {
                    file.errorMsg = response.status + ': ' + response.data;
                }
            }, function(evt) {
                file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
            });
        });
        return upload;
    };

    /**
     * Link the image with ID `imgId` to the context with the id `contextId`
     */
    images.linkImage = function(imgId, contextId) {
        var formData = new FormData();
        formData.append('imgId', imgId);
        formData.append('ctxId', contextId);
        httpPutFactory('api/image/link', formData, function(response) {
            var content = $translate.instant('snackbar.image-linked.success');
            snackbarService.addAutocloseSnack(content, 'success');
            images.getImagesForContext(contextId);
        });
    };

    images.unlinkImage = function(imgId, contextId) {
        httpDeleteFactory('api/image/link/' + imgId + '/' + contextId, function(response) {
            var content = $translate.instant('snackbar.image-unlinked.success');
            snackbarService.addAutocloseSnack(content, 'success');
            images.getImagesForContext(contextId);
        });
    };

    images.deleteFile = function(file, fileList) {
        httpDeleteFactory('api/image/' + file.id, function(response) {
            if(!response.error) {
                var content = $translate.instant('snackbar.image-deleted.success');
                snackbarService.addAutocloseSnack(content, 'success');
                var i = fileList.indexOf(file);
                if(i > -1) fileList.splice(i, 1);
            }
        });
    };

    images.getImages = function() {
        return httpGetPromise.getData('api/image').then(function(response) {
            return response;
        });
    };

    images.getAllImages = function(forceUpdate) {
        forceUpdate = forceUpdate || false;<commit_msg>Fixed linking files to contexts<commit_after>spacialistApp.service('fileService', ['$rootScope', 'httpPostFactory', 'httpGetFactory', 'httpGetPromise', 'httpPutFactory', 'httpPatchFactory', 'httpDeleteFactory', 'snackbarService', 'searchService', 'Upload', '$translate', '$timeout', '$state', function($rootScope, httpPostFactory, httpGetFactory, httpGetPromise, httpPutFactory, httpPatchFactory, httpDeleteFactory, snackbarService, searchService, Upload, $translate, $timeout, $state) {
    var images = {
        all: [],
        linked: [],
        upload: {}
    };

    images.tags = {
        visible: false
    };

    var lastTimeImageChecked = 0;

    images.getMimeType = function(f) {
        // check for extension before mime-type check
        var filename = f.filename;
        var mt = f.mime_type;
        if(filename) {
            var suffix = filename.substr(filename.lastIndexOf('.')+1);
            switch(suffix) {
                case 'dae':
                case 'obj':
                    return '3d';
            }
        }

        if(mt.startsWith('image/')) return 'image';
        if(mt == 'application/pdf') return 'pdf';
        if(mt == 'application/xml' || mt == 'text/xml') return 'xml';
        if(mt == 'application/xhtml+xml' || mt == 'text/html') return 'html';
        if(mt.startsWith('audio/')) return 'audio';
        if(mt.startsWith('video/')) return 'video';

        // default is simple text
        return 'text';
    };

    images.getImagesForContext = function(id) {
        if(!id) return;
        $rootScope.$emit('image:delete:linked');
        images.linked = [];
        images.getLinkedImages(id);
    };

    images.getLinkedImages = function(id) {
        if(!id) return;
        images.linked = filterLinkedImages(id);
    };

    images.loadImages = function(len, type) {
        var src = images[type];
        if(len == src.length) return src;
        var loaded = src.slice(0, len + 10);
        return loaded;
    };

    images.hasMoreImages = function(len, type) {
        var src = images[type];
        return src.length - len;
    };

    /**
     * Upload the image files `files` to the server, one by one and store their paths in the database.
     */
    images.uploadImages = function(files, invalidFiles, fileList) {
        var upload = {};
        upload.files = files;
        upload.errFiles = invalidFiles;
        upload.finished = 0;
        upload.toFinish = (typeof files === 'undefined') ? 0 : files.length;
        angular.forEach(files, function(file) {
            file.upload = Upload.upload({
                url: 'api/image/upload',
                data: { file: file }
            });
            file.upload.then(function(response) {
                $timeout(function() {
                    var data = response.data;
                    data.linked = [];
                    fileList.push(data);
                    upload.finished++;
                    if (upload.finished == upload.toFinish) {
                        $timeout(function() {
                            upload.files.length = 0;
                        }, 1000);
                        var content = $translate.instant('snackbar.image-upload.success', {
                            cnt: upload.finished
                        });
                        snackbarService.addAutocloseSnack(content, 'success');
                    }
                });
            }, function(response) {
                if(response.status > 0) {
                    file.errorMsg = response.status + ': ' + response.data;
                }
            }, function(evt) {
                file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
            });
        });
        return upload;
    };

    /**
     * Link the image with ID `imgId` to the context with the id `contextId`
     */
    images.linkImage = function(imgId, contextId) {
        var formData = new FormData();
        formData.append('imgId', imgId);
        formData.append('ctxId', contextId);
        httpPutFactory('api/image/link', formData, function(response) {
            var content = $translate.instant('snackbar.image-linked.success');
            snackbarService.addAutocloseSnack(content, 'success');
            $state.reload('root.spacialist');
        });
    };

    images.unlinkImage = function(imgId, contextId) {
        httpDeleteFactory('api/image/link/' + imgId + '/' + contextId, function(response) {
            var content = $translate.instant('snackbar.image-unlinked.success');
            snackbarService.addAutocloseSnack(content, 'success');
            images.getImagesForContext(contextId);
        });
    };

    images.deleteFile = function(file, fileList) {
        httpDeleteFactory('api/image/' + file.id, function(response) {
            if(!response.error) {
                var content = $translate.instant('snackbar.image-deleted.success');
                snackbarService.addAutocloseSnack(content, 'success');
                var i = fileList.indexOf(file);
                if(i > -1) fileList.splice(i, 1);
            }
        });
    };

    images.getImages = function() {
        return httpGetPromise.getData('api/image').then(function(response) {
            return response;
        });
    };

    images.getAllImages = function(forceUpdate) {
        forceUpdate = forceUpdate || false;
{'fraction_non_alphanumeric': 0.13206668529384372, 'fraction_numerical': 0.004284250721803111, 'mean_word_length': 1.9005942733657482, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6567405', 'n_tokens_mistral': 2997, 'n_tokens_neox': 2864, 'n_words': 581}